<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karthik Raja</title>
    <description>The latest articles on DEV Community by Karthik Raja (@karthikrg).</description>
    <link>https://dev.to/karthikrg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F862114%2Fb41328fd-4152-495b-aeb4-d56086034bde.png</url>
      <title>DEV Community: Karthik Raja</title>
      <link>https://dev.to/karthikrg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karthikrg"/>
    <language>en</language>
    <item>
      <title>Implementing Builder Pattern - Abstract Class</title>
      <dc:creator>Karthik Raja</dc:creator>
      <pubDate>Mon, 16 May 2022 17:23:32 +0000</pubDate>
      <link>https://dev.to/karthikrg/implementing-builder-pattern-abstract-class-3c1l</link>
      <guid>https://dev.to/karthikrg/implementing-builder-pattern-abstract-class-3c1l</guid>
      <description>&lt;h1&gt;
  
  
  &lt;u&gt;Builder Pattern&lt;/u&gt;
&lt;/h1&gt;

&lt;p&gt;Let's take a look why need builder pattern...&lt;/p&gt;

&lt;p&gt;When we have a Class with N number of fields to be initialized in the constructor, we end up with &lt;a href="https://www.vojtechruzicka.com/avoid-telescoping-constructor-pattern/"&gt;Telescoping Constructor&lt;/a&gt;. The problem with the telescoping constructor is its difficult to remember the order of arguments to be passed while calling the constructor.&lt;/p&gt;

&lt;p&gt;The Builder Pattern is an object creation design pattern which intent is to separate the construction of a complex object from its representation. By doing so, the same construction process can lead to different representations. &lt;/p&gt;

&lt;p&gt;The way to design this pattern is to create a internal static class named &lt;code&gt;&amp;lt;ClassName&amp;gt;Builder&lt;/code&gt;, the methods of this class should allow to set the fields. Finally we have a build() method which in turn calls the constructor of the outer class which takes the builder class object as an argument to create a new object.&lt;/p&gt;

&lt;p&gt;In the upcoming example we will look how to implement Builder Pattern with Abstract class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class Animals {

    private int legs;
    private int eyes;
    private int ears;

    //getters //toString

    //Constructor has parameter of type builder 

    public Animals(AnimalBuilder builder) {
        this.legs = builder.legs;
        this.eyes = builder.eyes;
        this.ears = builder.ears;
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the Builder class of Animal Abstract class, here in this example it is a inner class and the build() method should be abstract which must be implemented by subclasses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract static class AnimalBuilder {

        private int legs;
        private int eyes;
        private int ears;

        public AnimalBuilder setLegs(int legs) {
            this.legs = legs;
            return this;
        }

        public AnimalBuilder setEyes(int eyes) {
            this.eyes = eyes;
            return this;
        }

        public AnimalBuilder setEars(int ears) {
            this.ears = ears;
            return this;
        }

        public abstract Animals build();

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's extend the above &lt;strong&gt;abstract class&lt;/strong&gt; &lt;code&gt;Animals&lt;/code&gt; on to &lt;strong&gt;concrete class&lt;/strong&gt; &lt;code&gt;Dog&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Dog extends Animals{

    private int tail;

    public Dog(DogBuilder builder) {
        super(builder);
        this.tail = builder.tail;
    }

//getters and toString

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should extend the internal abstract class &lt;code&gt;AnimalBuilder&lt;/code&gt; of abstract class &lt;code&gt;Animal&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class DogBuilder extends Animals.AnimalBuilder {


        private int tail;

        public DogBuilder setTail(int tail) {
            this.tail = tail;
            return this;
        }

        @Override
        public Animals build() {
            return new Dog(this);
        }
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally below is the driver code.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;Note*:&lt;/u&gt; in the below example we need to down cast the object to Dog explicitly as the super class method return type is Animal. &lt;br&gt;
This can be avoided if we use generics.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dog dog = (Dog) new Dog.DogBuilder().setTail(1).setLegs(4).setEars(2).setEyes(2).build();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Output : Dog has 1 tail, 4 legs, 2 ears, 2 eyes&lt;/code&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>oop</category>
      <category>design</category>
    </item>
  </channel>
</rss>
