Builder of design patterns

Source: Internet
Author: User
Builder Pattern)
Once you hear this name, you may guess one or two points. A builder simply understands that it is something to create. It is just something that the builder model creates, not something that is more complex than a hacker. It is like building a house. You need to build a foundation, build a wall, fill cement, seal the top, and tile the last wall.
The builder design mode is a design mode that constructs complex objects. In a software system, it may be difficult to create a complex object. If we use a single method or a single object to create a complex object, when the created complex object changes, the entire system may face drastic changes. In this case, we need to break down the creation process of this complex object into several parts, each of which is composed of a certain number of algorithms.
However, sub-parts may change frequently. How can we ensure the stability of the overall creation? This requires the support of the builder mode. The builder mode separates the creation and representation of complex objects so that different representations can be created for the same build process.




Two days ago, I bought a new Huawei P6-T00, smart phone is smart, with traffic that called a fast ah !, It's just like a cloud flow. After a while, 50 m of traffic will disappear, and people really doubt whether mobile phone developers are bored with mobile companies... No way, no way to use a cell phone, no way to access the Internet. In the end, you can only compromise. Go to the business office to change the package. The following describes the dynamic zone Internet package:


Dynamic zone Internet plan? What kind of Internet package is it? There are more than N online packages. How can we express the diversified forms of online packages?
1. what we can be certain about is what makes up the final online plan (Service name, monthly consumption, free duration, traffic, and SMS ), I know the structure, but I don't know what the package looks like.


2. It is a static class diagram of the builder mode to implement the Internet package. The builder mode breaks down the construction process of complex objects, so that each component completes the work separately than the worker (single and independent functions, high cohesion ), the association between each component is not very close (loosely coupled). At the same time, the component and the representation are separated.







Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace builder {// package construction class abstract class mealbuilder {public abstract void buildname (); // service name public abstract void buildexpense (); // fixed cost public abstract void buildvoice (); // complimentary speech public abstract void buildflow (); // free traffic public abstract void buildmessage (); // complimentary SMS} // detailed plan construction class (18) Class meal_18builder: mealbuilder {public override void buildname () {console. writeline ("dynamic zone Internet package (18 yuan)");} public override void buildexpense () {console. writeline ("fixed fee: 18/Month");} public override void buildvoice () {console. writeline ("Complimentary VOICE: 30 minutes");} public override void buildflow () {console. writeline ("free traffic: 50 m");} public override void buildmessage () {console. writeline ("Complimentary SMS: 100 messages") ;}// detailed plan construction class (28) Class meal_28builder: mealbuilder {public override void buildname () {console. writeline ("dynamic zone Internet package (28 yuan)");} public override void buildexpense () {console. writeline ("fixed fee: 28/Month");} public override void buildvoice () {console. writeline ("Complimentary VOICE: 50 minutes");} public override void buildflow () {console. writeline ("free traffic: 100 m");} public override void buildmessage () {console. writeline ("Complimentary SMS: 150 messages") ;}// detailed plan construction class (38) Class meal_38builder: mealbuilder {public override void buildname () {console. writeline ("dynamic zone Internet package (38 RMB)");} public override void buildexpense () {console. writeline ("fixed fee: 38/Month");} public override void buildvoice () {console. writeline ("voice: 80 minutes");} public override void buildflow () {console. writeline ("free traffic: 200 m");} public override void buildmessage () {console. writeline ("Complimentary SMS: 200 messages") ;}// conductor class ctor {private mealbuilder meal; public director (mealbuilder meal) {This. meal = meal;} public void createmeal () {meal. buildname (); meal. buildexpense (); meal. buildvoice (); meal. buildflow (); meal. buildmessage () ;}} class program {static void main (string [] ARGs) {mealbuilder meal_28builder = new meal_28builder (); Director director_28 = new director (meal_28builder); notify (); console. writeline (); mealbuilder meal_38builder = new meal_38builder (); Director director_38 = new director (meal_38builder); director_38.createmeal ();}}}










Builder (Abstract builder ):It specifies an abstract interface for each part of a product object. In this interface, two methods are declared. One method is buildpartx (), which is used to create components of complex objects; another method is getresult (), which is used to return complex objects. Builder can be both an abstract class and an interface.


Concretebuilder (detailed builder ):It implements the builder interface, implements detailed construction and assembly methods of each component, defines and understands the complex objects it creates, and provides a method to return the created complex product objects.


Product (Product role ):It is a complex object to be built, including multiple components. The detailed builder creates an internal representation of the product and defines its assembly process.


Director (conductor ):The conductor, also known as the director class, is responsible for arranging the construction order of complex objects. There is an association between the conductor and the abstract builder, which can be in its construct () in the construction method, the component construction and assembly methods of the builder object are called to complete the construction of complex objects. Generally, the client only needs to interact with the conductor. In the client, determine the detailed builder type and instantiate the detailed builder object (through the configuration file and reflection mechanism ), then, the object is passed into the conductor class through the constructor or setter method.


Strengths of the builder model:
The builder separates the construction and representation, effectively decomposing the complex object processing process, reducing the Coupling Degree between functional modules, and enhancing the cohesion inside the module, this makes it an extremely important position in the software design model.


Disadvantages of builder mode:
1. the products created by the builder mode generally share many similarities, and their components are similar. If the differences between products are very large, for example, many components are not the same, it is not suitable for the builder mode, therefore, the scope of use is limited.
2. Assuming that the internal changes of the product are complex, it may lead to the need to define a lot of detailed builder classes to achieve such changes, resulting in a very large system, adding the difficulty of understanding the system and the execution cost.


Differences between builders and abstract factories:
Abstract factories and builders share similarities. They are both design patterns for creating complex objects. The difference is that the builders focus on building a complex object step by step, abstract Factory focuses on the construction of product objects of multiple series, namely, object families (simple or complex.



Usage
1. When the generated product object has a complex internal structure;
2. When complex objects need to be separated from the representation, different representations may need to be created;
3. When you need to hide the internal structure of the product to the customer.













Builder of design patterns

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.