Thursday, 15 March 2018

Design Pattern in Android -- Builder Pattern

Builder Pattern for Android

Builder pattern are very help-full when having model class, inside this model class having constructor with many parameters, which having some of them optional and some of them mandatory, by using builder-pattern, we will create chain of methods. The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a hand-full parameters.  

For example, if you observe in our in-build alert dialog,  AlertDialog using builder class to build alert. Code is look like:

new AlertDialog.Builder(this)
        .setTitle("Alert Dialog")
        .setMessage("Alert message")
        .setCancelable(false)
        .create();


So our question will be when and why we will use builder pattern?

Assume we have a model class which having list of constructors like bellow:

Student(int id, String name){...........}
Student(int id, String name,String stream){...........}
Student(int id, String name,String stream,String historyMarks){...........}
Student(int id, String name,String stream,String historyMarks,String mathMarks){...........}
..
...
.....
.......
...........

The problem is with this once constructors are 5 or 6 long parameters long, it's really very difficult to remember every-time particular constructor we should use. The better solution is to use builder-pattern.

Now we will create a builder class for better understanding. Firstly create a model class with name BuilderPatternModel.
   
Inside this model class, we will define some variables as bellow:

private String firstName;
private String lastName;
private int age;
private String phone;
private String address;


Now we will not generate any getter and setter methods, we will create UserBuilder class inside BuilderPatternModel which is static. UserBuilder have the methods to build our object. 

For example:
public static class UserBuilder{
private String fName;
private String lName;
private int ag;
private String ph;
private String addr;
    public UserBuilder fName(String fName){
    this.fName = fName;
    return this;
}
public BuilderPatternModel build() {

    BuilderPatternModel bModel = new BuilderPatternModel(this);
    return bModel;
}
}


In this above example, having setter type method but having UserBuilder return type and at last having build() which returns BuilderPatternModel. 

Now next important thing is:
Create a private constructor BuilderPatternModel look like bellow: 

private BuilderPatternModel(UserBuilder ub){
    this.firstName = ub.fName;
    this.lastName = ub.lName;
    this.age = ub.ag;
    this.phone = ub.ph;
    this.address = ub.addr;
}

This constructor setting value and making it private so it can't access from out side.

BuilderPatternModel class looking like bellow:

public class BuilderPatternModel {

    private String firstName;
    private String lastName;
    private int age;
    private String phone;
    private String address;


    private BuilderPatternModel(UserBuilder ub){
        this.firstName = ub.fName;
        this.lastName = ub.lName;
        this.age = ub.ag;
        this.phone = ub.ph;
        this.address = ub.addr;
    }

    public String getFirstName(){
        return firstName;
    }

    public String getLastName(){
        return lastName;
    }

    public int getAge(){
        return age;
    }

    public String getPhone(){
        return phone;
    }

    public String getAddress(){
        return address;
    }


    public static class UserBuilder {
        private String fName;
        private String lName;
        private int ag;
        private String ph;
        private String addr;


        public UserBuilder() {
        }

        public UserBuilder fName(String fName){
            this.fName = fName;
            return this;
        }

        public UserBuilder lName(String lName){
            this.lName = lName;
            return this;
        }

        public UserBuilder ag(int ag){
            this.ag = ag;
            return this;
        }

        public UserBuilder ph(String ph){
            this.ph = ph;
            return this;
        }

        public UserBuilder address(String address){
            this.addr = address;
            return this;
        }

        public BuilderPatternModel build() {

            BuilderPatternModel bModel = new BuilderPatternModel(this);
            return bModel;
        }

    }//End of User-Builder
}

How to use this model class for builder-pattern:

ArrayList<BuilderPatternModel> listModel = new ArrayList<>();
BuilderPatternModel m1 = new BuilderPatternModel.UserBuilder()
        .fName("Fname1")
        .lName("lName")
        .ag(10)
        .ph("123456")
        .address("addresss")
        .build();
BuilderPatternModel m2 = new BuilderPatternModel.UserBuilder()
        .fName("Fname2")
        .address("addresss")
        .build();
listModel.add(m1);
listModel.add(m2);

How you can get the value:

for (int i = 0; i < listModel.size(); i++) {
Toast.makeText(getApplicationContext(), listModel.get(i).getFirstName() + " \n" + listModel.get(i).getLastName()
+ " \n" + listModel.get(i).getAge() + " \n" + listModel.get(i).getPhone() + " \n" + listModel.get(i).getAddress(), Toast.LENGTH_SHORT).show();

}















   

No comments:

Post a Comment