Creation Mode-builder Mode

Source: Internet
Author: User
Using system; using system. text; using system. collections. generic; using system. LINQ; namespace builder mode {/** builder mode intent: separates the construction of a complex object from its representation, so that the same build process can create different representations. ** Builder mode advantages: 1. the builder mode allows independent changes to the internal representation of the product, so that the client does not have to know the internal composition details of the product. * 2. Each builder is relatively independent of other builders. * 3. The construction process can be controlled more precisely to reduce the risk of details. * 4. Build Code Separated from the representation code. ** Disadvantages of the builder mode: 1. Because the change points lie in the details of object creation, it is also difficult to build Algorithm Requirement changes because they focus on the order of object creation. ** Construction mode Implementation points: 1. the builder mode is mainly used to "build a complex object by step", where "Step by Step" is a stable algorithm, * various parts of complex objects are often changed. * 2. abstract classes are not required for products, especially when this mode is used because the algorithm for creating objects is complex or this mode is applied to the product generation process, * the final results may vary greatly, it is unlikely that an abstract product class is extracted. 3. The interface method used to create sub-parts in the Creator is not an abstract method, but an empty method. No operation is performed, the specific creator only needs to override the required method, * but this is not absolute, especially in the case of text conversion, the default method is a reasonable default operation for output with the input intact * 4. The Builder mode is often used in combination with the composite pattern mode, it focuses on the application scenarios of the object "partial changes" ** builder mode: 1. When creating complex objects, algorithms should be independent of the components of the object and their assembly methods. * 2. When the constructor must allow different representations of the object to be constructed. * 3. For the same method, different execution sequence, and different event structures with different parameters, consider the builder mode * 4. When multiple parts or parts can be assembled into an object, however, the running effect is not the same. * 5. The product class is very complex, or the calling sequence in the product class has different parameters. ** the builder mode produces the following results: 1. The Builder mode enables independent changes to the internal appearance of the product. By using the builder mode, the client does not have to know the details of the product composition. * 2. Separate the build code from the presentation code. ** Scaling of builder mode changes: you can omit the abstract buider ** // <summary> /// the product to be created /// </Summary> public class product {private ilist <string> parts = new List <string> (); public void addparts (string part) {parts. add (part);} public void show () {console. writeline ("Product Creation starts ...... "); foreach (string part in parts) {console. writeline ("part" + part);} console. writeline ("product creation completed") ;}/// <summary> /// abstract construction mode, which abstracts the process required to build a product /// </Summary> public abstract class builder {/// <summary> /// create the abstract of Part A /// </Summary> public abstract void buildparta (); /// <summary> /// create the abstract of Part B /// </Summary> public abstract void buildpartb (); /// <summary> /// obtain the created product /// </Summary> /// <returns> </returns> public abstract product getproduct ();} /// <summary> /// specific creator 1 /// </Summary> public class concretebuider1: builder {private Product Product = new product (); // <summary> // create a specific part A1 /// </Summary> Public override void buildparta () {product. addparts ("A1") ;}/// <summary> /// create a specific part B1 /// </Summary> Public override void buildpartb () {product. addparts ("B1");} public override product getproduct () {return product ;}} /// <summary> /// creation of the product /// </Summary> public class concretebuider2: builder {product Product = new Prod UcT (); Public override void buildparta () {product. addparts ("A2");} public override void buildpartb () {product. addparts ("B2");} public override product getproduct () {return product ;}/// <summary> // construct a command class and construct an object using the builder interface, that is, the specific builder role is called to create a product object, but the specific implementation of the product is not created. /// </Summary> public class director {public void construct (builder buider) {buider. buildparta (); buider. buildpartb () ;}} public class appclient {public static void main (string [] ARGs) {Director tor = new director (); builder buider1 = new concretebuider1 (); builder buider2 = new concretebuider2 (); // build the specific object Diretor through the command class. construct (buider1); product product1 = buider1.getproduct (); product1.show (); Diretor. construct (buider2); product product2 = buider2.getproduct (); product2.show ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.