Translate

Monday, July 20, 2015

Builder pattern tutorial Java (Beginners introduction to builder pattern)

The requirement

Suppose you need a Sandwich class which has 3 ingredients bread, cheese and meat. The requirement is you can pick any combination of these items. And once the Sandwich class is initialized, you shouldn't be able to change these ingredients. One easy way to achieve this is


public class Sandwich {

    private String bread;//immutable
    private String cheese;//immutable
    private String meat;//immutable

    public Sandwich(String bread, String cheese, String meat) {
        this.bread = bread;
        this.cheese = cheese;
        this.meat = meat;
    }

    public String getBread() {
        return bread;
    }

    public String getCheese() {
        return cheese;
    }


    public String getMeat() {
        return meat;
    }

}



public class Main {
   
public static void main(String[] args) {
        Sandwich mySandwich =
new Sandwich("ItalianBread", "Swiss", "");
    }
}

The above code doesn't let you change the ingredients once you initialize it, which meets our requirement at the same time you have to specify all the ingredients even if you don't need it. (We didn't want meat in our sandwich so we just passed an empty string for meat).


A more graceful code that uses builder pattern

A more graceful way of handling the above requirement is by using a builder pattern. In this pattern, we use a nested static class. 

public class Sandwich {
   
private String bread;// immutable
   
private String cheese;///immutable
   
private String meat;///immutable

   
public Sandwich(Builder builder) {
       
this.bread = builder.bread;
       
this.cheese = builder.cheese;
       
this.meat = builder.meat;
    }

   
public static class Builder {

       
private String bread;
       
private String cheese;
       
private String meat;

       
public Builder() {
        }

       
public Builder bread(String bread) {
           
this.bread = bread;
           
return this;//We are returning a reference to itself so that we can call other methods on it like cheese() meat() and so on
       
}

       
public Builder cheese(String cheese) {
           
this.cheese = cheese;
           
return this;
        }

       
public Builder meat(String bread) {
           
this.meat = meat;
           
return this;
        }

       
public Sandwich build() {
           
return new Sandwich(this
);
        }

    }


   
public String getBread() {
        
return bread;
    }

   
public String getCheese() {
       
return cheese;
    }

   
public String getMeat() {
       
return meat;
    }

}

public class Main {
    public static void main(String[] args) {
        Sandwich.Builder sandwichBuilder=new Sandwich.Builder();
        sandwichBuilder.bread("ItalianBread").cheese("swiss");
        Sandwich mySandwich=sandwichBuilder.build();
    }
}




No comments:

Post a Comment

Comments will appear once they have been approved by the moderator