Builder Pattern)
Once you hear this name, you may guess one or two points. The simple understanding of the builder is to create things, but the builder mode is not a simple thing, but a complicated thing. It is like building a house. You need to build a foundation, build a wall, fill cement, seal the top, and finally wall tiles.
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 during the same build process.
I bought a new one two days ago.Huawei P6-T00
Smart phones are smart. It's a fast way to use traffic !, 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, no way to use a mobile phone, no way to access the Internet, and finally only compromise, go to the business office to change the package. Next we will share with you 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. we can be certain that the final online package consists of the specific components (Service name, monthly consumption, free duration, traffic, and SMS ), that is, the structure is known, but what the package looks like is unknown.
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 can complete relatively independent work (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} // specific 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") ;}// specific package 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") ;}// specific package 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 generally declared. One method is buildpartx (), they are used to create various parts of a complex object. The other method is getresult (), which is used to return complex objects. Builder can be either an abstract class or an interface.
Concretebuilder: it implements the builder interface, implements the specific construction and assembly methods of each component, and defines and clarifies the complex objects it creates, you can also provide a method to return the created complex product object.
Product (Product role): It is a complex object to be built and contains multiple components. A specific 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 a correlation between the conductor and the abstract builder. It can be found in its construct () the component construction and assembly methods of the builder object are called in the Construction Method to complete the construction of complex objects. Generally, the client only needs to interact with the conductor, determine the type of the specific builder on the client, and instantiate the specific builder object (you can also use the configuration file and reflection mechanism ), then, the object is passed into the conductor class through the constructor or setter method.
Advantages of the builder mode:
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 large, for example, many components are different, it is not suitable for the builder mode, therefore, the scope of use is limited.
2. If the internal changes of the product are complex, many specific builder classes may need to be defined to implement such changes, resulting in a huge system and increasing the difficulty of understanding the system and operating costs.
Differences between builders and abstract factories:
Abstract factories and builders are similar in that 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 performance of the product's internal structure to the customer.