This article continues to introduce23 Design Mode Seriesthe builder model.
Definition:Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations.
Practical Range1. When creating complex objects, the algorithm should be independent of the parts of the object and how they are assembled. 2, when the construction process must allow the constructed object to have different representations.
roleIn this design pattern, there are several roles: 1, Builder: Specify an abstract interface for each part that creates a product object. 2. ConcreteBuilder: Implements the builder interface to construct and assemble the individual parts of the product, defines and clarifies the representations it creates, and provides an interface for retrieving the product. 3. Director: Constructs an object that uses the Builder interface to guide the build process. 4. Product: Represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, which contains the classes that define the constituent parts, including the interfaces that assemble the parts 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 ("Build Man's Body"); public void Buildfoot () { person.setfoot ("Build Man's Feet"); } public void Buildhead () { person.sethead ("Build 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 Woman's Body"); public void Buildfoot () { person.setfoot ("Build a woman's foot") ; public void Buildhead () { person.sethead ("Build Woman's Head"); Public Person Buildperson () { return person; }}
role Director:
public class Persondirector { 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 Mans extends person {public man () { System.out.println ("Start building Men");} } public class Woman extends man {public Woman () { System.out.println ("Start building 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 model can evolve in many forms during use: if there is only one concrete object to be built, the abstract builder and director can be omitted, allowing ConcreteBuilder to play the role of mentor and builder, Even ConcreteBuilder can be put into product. In the "Effective Java" book, the second article mentions "when encountering multiple constructor parameters to consider the builder", in fact, the builder here is the builder mode, but the inside of the four characters are put into the specific product inside.
The above example, if only a man, evolved as follows:
public class Mans { 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{ mans Man; Public Manbuilder () {Man = new Man (); } public void Buildbody () { man.setbody ("Build Man's Body"); public void Buildfoot () { man.setfoot ("Build Man's Feet"); } public void Buildhead () { man.sethead ("Build Man's Head"); } Public Mans Builderman () { buildhead (); Buildbody (); Buildfoot (); return man; }}
Test:
public class test{public static void Main (string[] args) { Manbuilder builder = new Manbuilder (); Mans man = Builder.builderman (); }}
More Design Patterns: 23 design Mode Series
jason0539
Blog: http://blog.csdn.net/jason0539 (reprint please indicate the source)
Sweep code Follow me public number
The builder pattern for Java design patterns