C # Design pattern (5)-builder mode

Source: Internet
Author: User

Introduction

The previous article introduced the abstract Factory mode-c# design pattern in design mode (3)-Abstract Factory mode, this article will introduce the builder model ;

Click here to view all design mode series articles navigation

Introduction to Builders ' models

Builder Mode is to separate the construction and presentation of a complex object so that the same build process can create different representations.

In the software system, sometimes faced with the creation of "a complex object ", which is usually made up of the sub-objects of each part with certain algorithms, because of the change of the requirements, the various parts of this complex object may face drastic changes , but the algorithm that combines them is stable. Provides a "encapsulation mechanism" to isolate the "various parts of complex objects" change, so that the "stable building algorithm" in the system does not change with the need to change.

Instance Scenario

For software companies, the company has research and development personnel (including administrative personnel, management personnel, etc.), every time the new recruits, the company's IT staff need to provide a computer for the staff, but different employees need different configuration of the computer, research and development personnel need to configure a higher computer, Administrative personnel need to configure the requirements are not so high, of course, there may be a lot of configuration, such as the installation of the company used for game games with the computer, UE engineers use the computer, do the video and so on the need for configuration may be the same; here we only use the development of computer, office computer for example;

However, regardless of the level of configuration needs to assign a new computer to the staff, the following we use this scenario to explain the builder model;

Instance Codeclass Diagram

built by

Builder abstract class, where the process of installing a computer is standardized

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebuilderpattern{/// <summary>    ///abstract installation of computer procedures/// </summary>     Public Abstract classComputerbuilder {/// <summary>        ///Installing the Memory strip/// </summary>         Public Abstract voidsetupmemory (); /// <summary>        ///installing the hard drive/// </summary>         Public Abstract voidSetupharddisk (); /// <summary>        ///Installing the operating system/// </summary>         Public Abstract voidSetupoperatingsystem (); //There are many other steps required to install the computer, such as installing the CPU, motherboard, etc., not all listed here    }}

If you install a computer that the developer uses

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebuilderpattern{/// <summary>    ///Installing a developer using a computer/// </summary>    Public classDevelopmentcomputerbuilder:computerbuilder {/// <summary>        ///Installing Memory/// </summary>         Public Override voidSetupmemory () {Console.WriteLine ("install a 12G memory strip (The development machine memory needs to be configured a little bit)"); }        /// <summary>        ///installing the hard drive/// </summary>         Public Override voidSetupharddisk () {Console.WriteLine ("install a 1T hard drive"); }        /// <summary>        ///Installing the operating system/// </summary>         Public Override voidSetupoperatingsystem () {Console.WriteLine ("Install WIN7 System (development machine needs to install various development tools, Win7 system is more stable)"); }    }}

If you install a computer for normal office use

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebuilderpattern{/// <summary>    ///Installing a normal office PC/// </summary>     Public classOfficecomputerbuilder:computerbuilder {/// <summary>        ///Installing Memory/// </summary>         Public Override voidSetupmemory () {Console.WriteLine ("install a 4G memory strip"); }        /// <summary>        ///installing the hard drive/// </summary>         Public Override voidSetupharddisk () {Console.WriteLine ("Installing a 500G hard drive"); }        /// <summary>        ///Installing the operating system/// </summary>         Public Override voidSetupoperatingsystem () {Console.WriteLine ("Installing the WIN10 system"); }    }}

Of course, there may be a lot of configuration, such as the installation of the company used for game games with the computer, UE engineers use the computer, do video and so on need to configure the configuration may be the same;

Conductor

The purpose of the conductor class is to install the computer according to the different needs of the user, while the person or users do not need to know the installation process, and each of these steps is necessary;

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebuilderpattern{classComputerdirector { Public voidConstructcomputer (Computerbuilder builder) {//Installing the Memory stripBuilder.            Setupmemory (); //installing the hard driveBuilder.            Setupharddisk (); //Installing the operating systemBuilder.        Setupoperatingsystem (); }    }}
Business Call
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacebuilderpattern{classProgram {Static voidMain (string[] args) {Console.WriteLine ("----------------Start installing the computer-------------------------------------"); //developers use computers to install buildersComputerbuilder Builder =NewDevelopmentcomputerbuilder (); //General office Use computer installation builder//Computerbuilder builder = new Officecomputerbuilder ();Computerdirector Director =NewComputerdirector (); Director.            Constructcomputer (builder); Console.WriteLine ("----------------Installation is complete--------------------------------");        Console.readkey (); }    }}

If the installation and development of computer use, the results are as follows

If you install a normal office using a computer, the results are as follows

Builder pattern Structure diagram

In this example:

Builder is an abstract class that installs computers;

Constructbuilder for concrete builders, that is, the above-mentioned Office computer installation, research and development with computer installation, all specifically to achieve the computer installation abstract class;

Product products are specific computers;

Derictor, conductor, the above example is installed according to the specific needs of users of different configurations of the computer;

Summary Applicable Scenarios

1. The product objects that need to be generated have complex internal structures that typically contain multiple member properties.
2. Isolate the creation and use of complex objects, and allow the same creation process to create different products.

The builder model creates products that generally have more in common and have similar components, and if the differences between the products are large, it is not appropriate to use the builder model.

Pros and cons

Advantages:

The use of the builder model allows the internal representation of the product to change independently. Using the builder mode allows the client not to know the details of the internal composition of the product.

The creation of complex products is decomposed into different methods, which makes the creation process clearer and allows us to control the generation of complex objects more precisely.

Each builder is relatively independent and not related to the other builder (a product has a builder counterpart)and can be easily added or replaced by builders.

Disadvantages:

The builders ' shortcomings in the complex with the internal changes of the product may result in the need to define many concrete builder classes to achieve this change, resulting in a system that is so large that this pattern does not apply.

C # Design pattern (5)-builder mode

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.