The builder pattern in the big talk design model refers to an example: McDonald's, KFC's hamburger no matter which store to eat, what time to eat, at least in China, the taste is basically the same. And that "fish incense shredded pork" is almost all the size of the restaurant restaurant has a dish, but can eat a myriad of flavors, this is why?
Builder Pattern: Separates the construction of a complex object from its representation so that the same build process can create different representations.
Builder Pattern Structure diagram
- Builder is an abstract interface that is specified to create individual parts of a product object.
- ConcreteBuilder are concrete builders that implement builder interfaces to construct and assemble individual components.
- product is a production role
- The director is the conductor who constructs an object that uses the builder interface.
Product class, which consists of several parts.
public class product{IList <String> parts = new List<string> (); public void ADD (String part) {Parts.add (part); public void Show () { // product creation for (List list:parts) { //
println list
The builder class, the abstract builder class, determines that the product consists of two parts Parta and PARTB, and declares a method getresult the result of the product being built.
Public Abstract class builder{ publicabstractvoid Buildparta (); Public Abstract void BUILDPARTB (); Public Abstract Product GetResult ();}
ConcreteBuilder Class--Concrete construction class
Public classConcreteBuilder1extendsbuilder{PrivateProduct Product =NewProduct (); Public voidbuildparta{//Part AProduct. ADD ("Attachment A"); } Public voidbuildpartb{//Annex BProduct. ADD ("Attachment B"); } PublicProduct GetResult () {returnproduct; } }
ConcreteBuilder2 class--Concrete builder class
Public classConcreteBuilder2extendsbuilder{PrivateProduct Product =NewProduct (); Public voidbuildparta{//Part xProduct. ADD ("Attachment x"); } Public voidbuildpartb{//Attachment yProduct. ADD ("Attachment y"); } PublicProduct GetResult () {returnproduct; } }
Director class--directing this kind of
Public class director{ publicvoid Construct (builder builder) { Builder. Buildparta (); Builder. BUILDPARTB (); }}
Client-side code, the customer does not need to know the specific construction process
public class client{ static void main (string args) {Director Director = new director (); Builder B1 = new ConcreteBuilder1 (); Builder b2 = new ConcreteBuilder2 (); Director. Construct (B1); Product p1 = B1. GetResult (); P1.show (); Director. Construct (B2); Product p2 = B2. GetResult (); P2.show (); } }
The builder pattern is the pattern that is applied when the algorithm that creates the complex object should be independent of the part of the object and how they are assembled.
Builder model Scope of application
The benefit of the builder model is to separate the construction code from the presentation code , since the builder hides how the product is assembled, so if you need to change the internal representation of a product , you only need to define a specific builder You can do it.
Good food each aftertaste different-builder mode