Design Mode (9) --- builder mode, design mode --- Construction

Source: Internet
Author: User

Design Mode (9) --- builder mode, design mode --- Construction

I. Definition

Builder mode: separates the construction of a complex object from its representation, so that different representations can be created during the same build process.

Explanation: In the builder mode, you only need to specify the types to be constructed to obtain them, but you do not need to know the specific construction process and details.

 

Ii. UML class diagram

III. Basic Code

class Program    {        static void Main(string[] args)        {            Director director = new Director();            Builder builder1 = new ConcreteBuilder1();            director.Construct(builder1);            Product product = builder1.GetProduct();            product.Show();            Console.Read();        }    }    class Product    {        IList<string> parts = new List<string>();        public void Add(string part)        {            parts.Add(part);        }        public void Show()        {            Console.WriteLine("\n product create");            foreach (string part in parts)            {                Console.WriteLine(part);            }        }    }    abstract class Builder    {        public abstract void BuildPartA();        public abstract void BuildPartB();        public abstract Product GetProduct();    }    class ConcreteBuilder1 : Builder    {        private Product product = new Product();        public override void BuildPartA()        {            product.Add("part A");        }        public override void BuildPartB()        {            product.Add("part B");        }        public override Product GetProduct()        {            return product;        }    }    class Director    {        public void Construct(Builder builder)        {            builder.BuildPartA();            builder.BuildPartB();        }    }

In the basic code, the ctor conductor directs the builder to create the product. All specific builders inherit the interface class builder to create a specific product. In the client code, the conductor and the specific Builder are instantiated, and the conductor directs the specific builder to create the product to complete the functions of the production product.

 

Iv. Specific instances

Specific requirements: the buyer (client) needs to purchase a batch of computers. He will go to the computer city to find the boss (conductor) instructions, and the boss will arrange personnel to assemble the computers according to the customer's requirements. Throughout the entire process, the buyer will not involve complex engineering of computer assembly. He is only responsible for raising demands and then purchasing the computer.

Product computers:

Public class Computer {private IList <string> parts = new List <string> (); public void Add (string part) {parts. add (part);} public void Show () {Console. writeLine ("computer assembly starts ................. "); foreach (string part in parts) {Console. writeLine ("component" + part + "installed");} Console. writeLine ("Computer assembled ");}}

 

Abstract builder:

public abstract class Builder    {        public abstract void BuildPartCPU();        public abstract void BuildPartMainBoard();        public abstract Computer GetComputer();    }

 

Specific builder:

public class ConcreteBuilder1 : Builder    {        Computer computer = new Computer();        public override void BuildPartCPU()        {            computer.Add("CPU");        }        public override void BuildPartMainBoard()        {            computer.Add("main board");        }        public override Computer GetComputer()        {            return computer;        }    }

 

Conductor boss:

public class Director    {        public void Construct(Builder builder)        {            builder.BuildPartCPU();            builder.BuildPartMainBoard();        }    }

 

Client:

Director director = new Director();            Builder builder1 = new ConcreteBuilder1();            director.Construct(builder1);            Computer computer1 = builder1.GetComputer();            computer1.Show();

 

V. simplified example:

In a specific application, either the conductor or the abstract builder can be omitted. In this example, only the product class and builder are allowed:

Product Type:

Public class Product {private IList <string> parts = new List <string> (); public void Add (string part) {parts. add (part);} public void Show () {Console. writeLine ("product assembly starts ................. "); foreach (string part in parts) {Console. writeLine ("component" + part + "installed");} Console. writeLine ("Product Assembled ");}}View Code

 

Builder:

Public class Builder {private Product product = new Product (); public void BuildPartA () {product. add ("PartA");} public void BuildPartB () {product. add ("PartB") ;}public Product GetProduct () {return product;} public void Construct () {BuildPartA (); BuildPartB ();}}View Code

 

Client:

builder = new Builder();            builder.Construct();            Product product = builder.GetProduct();            product.Show();

 

Vi. Summary

Steps for implementing the builder mode: 1) Compiling the product class; 2) Compiling the abstract builder class (which can be omitted); 3) Compiling the specific builder; 4) Compiling the conductor class command builder how to assemble the product; finally, the client can call it. The builder mode decouples the assembly process and specific components by separating the build code from the presentation code. In addition, the internal details of how to assemble the client are hidden, so that the client does not have to worry about how to assemble, just need to use.


What are the design patterns?

There are three types of design patterns: creation, structure, and behavior.
The creation types include:
I. Singleton, Singleton mode: ensure that a class has only one instance and provide a global access point to it.
2. Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
3. Factory Method: Define an interface used to create objects, and let the subclass decide which class to instantiate. Factory Method delays the instantiation of a class to the subclass.
4. Builder: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
5. Prototype: Use a Prototype instance to specify the type of the object to be created, and copy the Prototype to create a new object.
Behavior types:
6. Iterator: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.
7. Observer: Observer mode: defines one-to-many dependencies between objects. When the status of an object changes, all objects dependent on it will be automatically updated by notification.
8. Template Method: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass, templateMethod allows the subclass to redefine a specific step without changing the structure of an algorithm.
9. Command: encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests and record request logs, and supports unrecoverable operations.
10. State: allows an object to change its behavior when its internal State changes. The object seems to have changed its class.
11. Strategy: Define a series of algorithms, encapsulate them one by one, and enable them to replace each other. This mode allows algorithms to be independent of customers who use them.
12. China of Responsibility, Responsibility chain mode: Enables multiple objects to process requests to avoid coupling between the request sender and receiver.
13. Mediator: uses an intermediary object to encapsulate object interaction of some columns.
14. Visitor, Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on this element without changing the element classes.
15th, Interpreter, Interpreter mode: a language is defined to define a representation of its grammar and an Interpreter. This Interpreter uses this representation to explain sentences in the language.
16. Memento: capture the internal state of an object without interrupting the object, and save the state outside the object.
There are:
17. Composite: Composite combines objects into a tree structure to represent the relationship between parts of the whole. Composite makes the use of a single object and a Composite object consistent.
18. Facade, appearance mode: provides a consistent interface for a group of interfaces in the subsystem. fa? Ade provides a high-level interface, which makes the subsystem easier to use.
19. Proxy: provides a Proxy for other objects to control access to this object.
20. Adapter: the Adapter mode converts a class of interfaces into another interface that the customer wants. The Adapter mode makes those classes unable to work together due to interface incompatibility.
21. Decrator: the Decorator mode dynamically adds some additional responsibilities to an object. In terms of the added functions, the Decorator mode is more flexible than the subclass generation mode.
22. Bridge: link the abstract Part with its implementation... the remaining full text>

In Java, what are the 23 design patterns that must be understood?

1. Simple Factory Pattern)
2. Builder Pattern)
3. Strategy Mode
4. Factory Method Pattern)
5. Abstract Factory)
6. Command Pattern)
7. Template Method)
8. Single Pattern)
9. Prototype Pattern)

Related Article

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.