Android Design Patterns - Builder Patterns

What's this

Builder pattern is a new generation of builder pattern, which separates the construction of complex objects from his representation, so that the same construction process can create different representation.

What are you having?

  1. Classical Construction Model
  • The product to be created (that is, the object)
  • Abstract build, the process of setting up
  • Implementation of abstract builder by concrete implementation of build
  • User
  1. Variant builder model
  • Product class
  • Internal builder

How do you do it?

Classic builder

  1. Create a product class Person
public class Person{
    private String career;//Occupation
    private String name;
    private String sex;
    private int age;
    public String getName(){
        return name ;
    }
    poblic String getSet(){
        return sex;
       
    }
    public int getAge(){
        return age;
    }
}
  1. builder interface
public inerface PersonBuiler{
    public void career();
    public void name();
    public void sex();
    public voide age();
    Person getPerson();
}
  1. builder implementation
    Realization of Teachers
public class TeacherBuilder implement PersonBuild(){
   private Person person;
   public TeacherBuilder(){
       person=new Person();
   }
   public void career(){
       person.setCareer("Teacher");
   }
    public void name(){
        person.setName("Mr. Zhang");
    }
    public void sex(){
        person.setSex("male");
    }
    public voide age(){
        person.setAge(35);
    }
    Person getPerson(){
        return person;
    }
}

Realization of Student Class

public class StudentBuilder implement PersonBuild(){
   private Person person;
   public TeacherBuilder(){
       person=new Person();
   }
   public void career(){
       person.setCareer("Student");
   }
    public void name(){
        person.setName("Zhang San");
    }
    public void sex(){
        person.setSex("male");
    }
    public voide age(){
        person.setAge(18);
    }
    Person getPerson(){
        return person;
    }
}
  1. User Director, holds builder objects, creator methods, returns products
public class Director{
    private PersonBuilder builder;
    public void setBuilde(PersonBuilder builder){
        this.builder=builder;
    }
    public void creater(){
        builder.career();
        builder.sex();
        builer.name();
        builder.age();
    }
    public Person getPerson(){
        return builder.getPerson();
    }
}
  1. Demonstration
Create a Student Class
Director d=new Director();
d.setBuilder(new StudentBuilder());
d.creater();
Person p=d.getPerson();

Variant builder

public class Person{
    private String career;//Occupation
    private String name;
    private String sex;
    private int age;
    public String getName(){
        return name ;
    }
    poblic String getSet(){
        return sex;
       
    }
    public int getAge(){
        return age;
    }
    private Person(Builder builder ){
        this.sex=builder.sex;
        this.age=builder.age;
        this.name=builder.name;
        this.career=builder.career;
    }
    public static class Builer(){
        private final String career;//Occupation
        private String name;
        private String sex;
        private int age;
        public Builder(String career){
            this.career=career;
        }
        public Builder setSex(String sex){
            this.sex=sex();
            return this;
        }
        public Builder setAge(int age){
            this.age=age;
            return this;
        }
        public Builder setName(String name){
            this.name=name ;
            retuen this;
        }
        public Person build(){
            return new Person(this);
        }
    }
}

Demonstration
​~~~java
Person p=new Person.builder("teacher"). setSex("man"). builder();

## When to use it
 1. When you need to create an object that requires a combination of many variables, you can consider separating the creation process of the object from the representation of the object, so that you can construct a complex object very succinctly.

Advantages and disadvantages
 Advantage:
(1) Good encapsulation, the use of builder mode can make the client do not need to know the details of the internal composition of the product; 
(2) The builder is independent and easy to expand; 
(3) Some other objects in the system will be used in the process of object creation, which are not easy to obtain in the process of product object creation.

Disadvantages:
(1) Redundant Builder objects and Director objects will be generated, which consumes memory; 
(2) Exposure of object construction process.. 

Keywords: Java

Added by corbin on Sun, 12 May 2019 09:20:17 +0300