Java Design Pattern builder Pattern
This article continues to introduce the builder modes of the 23 design patterns series.
Definition: builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Scope 1: When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods. 2. When the constructor must allow different representations of the constructed object.
In this design mode, a role has the following roles: 1. Builder: specify an abstract interface for each part of a product object. 2. ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, define and define the representation it creates, and provide an interface for retrieving the product. 3. Director: constructs an object using the Builder interface to guide the construction process. 4. Product: a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.
Role Builder:
public interface PersonBuilder { void buildHead(); void buildBody(); void buildFoot(); Person buildPerson();}
Role ConcreteBuilder:
Public class ManBuilder implements PersonBuilder {Person person; public ManBuilder () {person = new Man ();} public void buildbody () {Person. setBody (Building a man's body);} public void buildFoot () {person. setFoot (building man's feet);} public void buildHead () {person. setHead (create a man's head);} public Person buildPerson () {return person ;}}
Role ConcreteBuilder:
Public class WomanBuilder implements PersonBuilder {Person person; public WomanBuilder () {person = new Woman ();} public void buildbody () {person. setBody ("build a woman's body);} public void buildFoot () {person. setFoot ("build a woman's foot);} public void buildHead () {person. setHead ("build a woman's head);} public Person buildPerson () {return person ;}}
Role Director:
public class PersonDirector { public Person constructPerson(PersonBuilder pb) { pb.buildHead(); pb.buildBody(); pb.buildFoot(); return pb.buildPerson(); }}
Role Product:
Public class Person {private String head; private String body; private String foot; public String getHead () {return head;} public void setHead (String head) {this. head = head;} public String getBody () {return body;} public void setBody (String body) {this. body = body;} public String getFoot () {return foot;} public void setFoot (String foot) {this. foot = foot;} public class Man extends Person {public Man () {System. out. println ("start building man);} public class Woman extends Person {public Woman () {System. out. println ("start to build a woman );}}
Test:
public class Test{ public static void main(String[] args) { PersonDirector pd = new PersonDirector(); Person womanPerson = pd.constructPerson(new ManBuilder()); Person manPerson = pd.constructPerson(new WomanBuilder()); }}
The Builder mode can evolve into multiple forms during use: if there is only one object to be built, the abstract Builder and ctor can be omitted, so that ConcreteBuilder can assume the dual role of the mentor and Builder, even ConcreteBuilder can be implemented in the Product. In the second article in objective Java, we mentioned that "we should consider using the builder when encountering multiple constructor Parameters". In fact, the builder here belongs to the builder mode, only four roles are put in a specific product.
In the above example, if only men are manufactured, the evolution is as follows:
public class Man { private String head; private String body; private String foot; public String getHead() { return head; } public void setHead(String head) { this.head = head; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getFoot() { return foot; } public void setFoot(String foot) { this.foot = foot; }}
Public class ManBuilder {Man man; public ManBuilder () {man = new Man ();} public void buildbody () {man. setBody (Building a man's body);} public void buildFoot () {man. setFoot (building man's feet);} public void buildHead () {man. setHead (Building a Man's head);} public man builderMan () {buildHead (); buildBody (); buildFoot (); return Man ;}}
Test:
public class Test{ public static void main(String[] args) { ManBuilder builder = new ManBuilder(); Man man = builder.builderMan(); }}
More design patterns: 23 Design Patterns