[Mode overview] ---------- by xingoo
Pattern intent
The creation of an object is very complex. In order to distinguish the building process from the use process, it is separated. A ctor class is used to create objects. Builder specifies the creation process.
Mode Structure
Builder abstracts the builder interface and standardizes the construction of each component.
Concretebuilder is a specific builder role that builds components and provides examples.
Product role to create the returned object.
The Director role is responsible for creating and using director.
Use Cases
1. When creating an object is complex and independent from its use process.
2. The construction process allows the constructed objects to have different representations.
Code structure
Builder Interface
1 abstract class Builder{2 public abstract void buildPart1();3 public abstract void buildPart2();4 public abstract Product retrieveResult();5 }
Concretebuilder Product
1 class ConcreteBuilder extends Builder{ 2 public void buildPart1() { 3 4 } 5 public void buildPart2() { 6 7 } 8 public Product retrieveResult() { 9 return null;10 }11 }
Product
1 class Product{2 //Anything3 }
Director
1 class Director{2 private Builder builder;3 public void Constructor(){4 builder = new ConcreteBuilder();5 builder.buildPart1();6 builder.buildPart2();7 builder.retrieveResult();8 }9 }
Design Patterns in life
'The Transformers 4 has been around for almost half a year in recent years, but they still like the first actress.
Captain Optimus Prime, the classic line, and the transformation of Automotive people are also accompanied by a complicated process of change. The transformation of Automotive people is only in the twinkling of an eye, and it seems like the sky is flowing, which is a bit of the taste of the Creator. After complex changes, the various body parts of Optimus Prime finally become automotive people.
1/** 2 * builder 3 */4 interface transformer {5 Public void create_head (); 6 Public void create_body (); 7 8 Public Autobots transforming (); 9} 10/** 11 * concretebuilder12 */13 class transformer_captain implements transformer {14 public void create_head () {15 system. out. println ("shaping the head... "); 16} 17 Public void create_body () {18 system. out. println ("deformation... "); 19} 20 public Autobots transforming () {21 Return new Autobots (); 22} 23} 24/** 25 * product26 */27 class Autobots {28 Autobots () {29 system. out. println ("Ah !... Transformer .... "); 30} 31} 32/** 33 * this class is treated as director34 */35 public class transformerbuilder {36 37 public static Autobots transforming (transformer optimusprime) {38 optimusprime. create_head (); 39 optimusprime. create_body (); 40 return optimusprime. transforming (); 41} 42 43 public static void main (string [] ARGs) {44 transformer optimusprime = new transformer_captain (); 45 transforming (optimusprime); 46} 47 48}
Transformation Result
Deformation of the head... deformation of the body !... Transformer ....
[Design mode] -- creator mode Builder