The builder mode is also called the generator mode. It is an object creation mode used to hide the process of creating a composite object. It abstracts the process of creating a composite object and uses subclass inheritance or overloading, dynamically create objects with composite attributes.
Application scenarios:
-Object creation: the builder mode is designed for object creation.
-Create a composite object: The created object is a composite object with composite attributes.
-Follow the creation process of each part of an object: different factories (Builder here) have different methods to create product properties.
Composite attribute object (that is, the object to be created ):
public class House {private String floor;private String wall;private String proof;public String getFloor() {return floor;}public void setFloor(String floor) {this.floor = floor;}public String getWall() {return wall;}public void setWall(String wall) {this.wall = wall;}public String getProof() {return proof;}public void setProof(String proof) {this.proof = proof;}}
Specific Creator interface or abstract class:
public interface HouseBuilder {public void makeFloor();public void makeWall();public void makeProof();public House getHouse();}
Specific creator implementation Class 1:
public class ApartmentBuilder implements HouseBuilder {private House house = new House();@Overridepublic House getHouse() {return house;}@Overridepublic void makeFloor() {house.setFloor("Apartment --> Floor");}@Overridepublic void makeWall() {house.setWall("Apartment --> Wall");}@Overridepublic void makeProof() {house.setProof("Apartment --> Proof");}}
Specific creator implementation Class 2:
public class VillaBuilder implements HouseBuilder {private House house = new House();@Overridepublic void makeFloor() {house.setFloor("Villa --> Floor");}@Overridepublic void makeWall() {house.setWall("Villa --> Wall");}@Overridepublic void makeProof() {house.setProof("Villa --> Proof");}@Overridepublic House getHouse() {return house;}}
Designers:
public class HouseDirector {public void makeHouse(HouseBuilder builder) {builder.makeFloor();builder.makeProof();builder.makeWall();}}
Client call class:
public class ClientApp {public static void main(String[] args) {//HouseBuilder builder = new ApartmentBuilder();HouseBuilder builder = new VillaBuilder();HouseDirector director = new HouseDirector();director.makeHouse(builder);House house = builder.getHouse();System.out.println(house.getFloor());System.out.println(house.getProof());System.out.println(house.getWall());}}
Builder mode of Design Pattern