If we need to build a house, and we don't know how to build a house, we will find someone else to help us build a house.
Step 1:
Create a new House class House with the property of the House. Let's find the House builder interface HouseBuilder. If we want to build a bungalow, we will find PingFangHouseBuilder, which inherits from HouseBuilder, there are various methods to build a house, such as floor makeFloor and wall makeWall.
Step 2:
It's not enough for someone who can build a house. We need a dedicated designer, housedire, to call the house building method.
Step 3:
The customer only knows how to build a bungalow. He can only ask the designer to call the builder to build the house and then get the house from the builder.
The Code is as follows:
House. java
.floor = .wall = .housetop =
HouseBuilder. java
PingFangHouseBuilder. java
House house =
HouseDirector. java
MainClass. java
HouseBuilder houseBuilder = = =
1. What is the builder model?
The Builder mode is also called the Builder mode or Builder mode. It is one of the 23 design modes proposed by GoF. Builder mode is one of the object creation modes. It is used to hide the process of creating a composite object. It abstracts the process of creating a composite object and uses subclass inheritance and overloading, dynamically create objects with composite attributes.
II. Application scenarios of builder Mode
-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.