Today will be another article about the design patterns - builder (builder) mode, online information, there are many, but the feeling is the same large, small and different, hope Benpian can add points to the builder pattern for everyone awareness and understanding.
Introduction
1.1 Builder (builder) mode
Builder (builder) - Builder mode, the object is to build a complex represented by the phase separation, is of the same construction process can create different expressed.
A complex object into a plurality of simple objects, and step is constructed. Reached becomes not disguise the separation, i.e. the components are the same, but each part is flexible and can reach selected.
Builder mode hides the creation of complex objects, complex objects will be created in the process of abstraction, by sub-category basis or overloaded way, in line with dynamically created objects have properties.
1.2 Builder advantages and disadvantages
advantage
- Each specific extension builders are independent, the system facilitates
- The client does not have to know the internal implementation of specific products
Shortcoming
- Products must be part of the same, limiting the scope of use
- If the product of complex internal changes, add more builders category
Concerns Builder mode and factory pattern is different, Builder model is more emphasis on the process of assembling product parts, and factory pattern pay more attention to the process of creating product parts , both of which can be used in conjunction with the
1.3 application scenarios
- Creating and using a different isolation complex objects, the same method execution order, yielding a result of different events
- When the object is initialized, many parameters or many parameters have default values
- Not suitable for a lot of products to create differentiated class, if the product is responsible for internal changes will lead to the definition of many builders to achieve change, increase the number of classes, increased operating costs
Structure and implementation
Builder mode have a product specific constructors, constructors, and abstract commander of four elements.
2.1 Roles
Builder mode main roles are as follows
- Product role (Product): is a complex object that contains multiple components, which have specific builder to create their own components
- Abstract builder (Builder): Create a respective sub-product of a member interface includes an abstract method, further comprising a generally back to the product of the method The getProduct ()
- DETAILED creation method used to implement the interface Builder, complete complex products of the respective sub-components: the specific configuration by (Concrete Builder)
- Director (Director): completion of complex object creation and assembly, the command will not be involved in the product-specific information.
2.2 Structure
UML class diagram and
Structure Code
2.2.1 Product Role: complex objects comprising a plurality of components
class Product { private String partA; private String partB; private String partC; public void setPartA(String partA) { this.partA=partA; } public void setPartB(String partB) { this.partB=partB; } public void setPartC(String partC) { this.partC=partC; } public void show() { //显示产品的特性 } }
2.2.2 abstract builder: contains abstract methods to create various sub-components
abstract class Builder { // create product objects protected Product Product = new new Product (); public abstract void buildPartA (); public abstract void buildPartB (); public abstract void buildPartC (); // returns the product object public Product getResult () { return Product; } }
2.2.3 Specific Builders: builders to achieve abstract
public class ConcreteBuilder extends Builder { public void buildPartA() { product.setPartA("建造 PartA"); } public void buildPartB() { product.setPartA("建造 PartB"); } public void buildPartC() { product.setPartA("建造 PartC"); } }
2.2.4 commander: Call builder method to complete the creation of complex objects.
class Director { private Builder builder; public Director(Builder builder) { this.builder=builder; } //产品构建与组装方法 public Product construct() { builder.buildPartA(); builder.buildPartB(); builder.buildPartC(); return builder.getResult(); } }
2.2.5 Client
public class Client { public static void main(String[] args) { Builder builder=new ConcreteBuilder(); Director director=new Director(builder); Product product=director.construct(); product.show(); } }
Example: Create an example of a shared bicycle with Builder mode
3.1 Product categories: complex objects comprising a plurality of components
Including architecture car seats and tires.
public class Bike { private IFrame frame; private ISeat seat; private ITire tire; public IFrame getFrame() { return frame; } public void setFrame(IFrame frame) { this.frame = frame; } public ISeat getSeat() { return seat; } public void setSeat(ISeat seat) { this.seat = seat; } public ITire getTire() { return tire; } public void setTire(ITire tire) { this.tire = tire; } }
3.2 Builder: create a product comprising various subcomponents of a method of abstract interfaces
The product contains the return method getProduct ()
// 抽象 builder 类 public abstract class Builder { abstract void buildFrame(); abstract void buildSeat(); abstract void buildTire(); abstract Bike createBike(); }
3.3 ConcreteBuilder categories: used to implement the interface Builder
DETAILED complete complex method of creating the various subcomponents Product
// 具体 builder 类 public class MobikeBuilder extends Builder{ private Bike mBike = new Bike(); @Override void buildFrame() { mBike.setFrame(new AlloyFrame()); } @Override void buildSeat() { mBike.setSeat(new DermisSeat()); } @Override void buildTire() { mBike.setTire(new SolidTire()); } @Override Bike createBike() { return mBike; } } public class OfoBuilder extends Builder{ private Bike mBike = new Bike(); @Override void buildFrame() { mBike.setFrame(new CarbonFrame()); } @Override void buildSeat() { mBike.setSeat(new RubberSeat()); } @Override void buildTire() { mBike.setTire(new InflateTire()); } @Override Bike createBike() { return mBike; } }
3.4 Command categories: completed complex object creation and assembly
Command does not involve product-specific information.
public class Director { private Builder mBuilder = null; public Director(Builder builder) { mBuilder = builder; } public Bike construct() { mBuilder.buildFrame(); mBuilder.buildSeat(); mBuilder.buildTire(); return mBuilder.createBike(); } }
3.5 client calls
public class Click { public static void main(String[] args) { showBike(new OfoBuilder()); showBike(new MobikeBuilder()); } private void showBike(Builder builder) { Director director = new Director(builder); Bike bike = director.construct(); bike.getFrame().frame(); bike.getSeat().seat(); bike.getTire().tire(); } }
Extended summary
Builder mode can be changed as required in the application process, if the product is only one class creates only need a specific builder, the builder may omit the abstract, or even remove the role of the commander.
Through the above explanation, I hope you design patterns - an understanding Builder mode - constructors (builders)! ! !