C # design mode-builder Mode

Source: Internet
Author: User

Intention

 

Separates a complex build from its representation so that different representations can be created during the same build process.

 

Scenario

 

There is always such an experience in Computer City installation. When we arrive at the store, a salesperson will first ask you about the configuration of the machine you want to install. He will give you some suggestions and eventually form an installation ticket. After confirming the installation configuration with the customer, he will hand over this word to the pick-up person who will prepare the accessories and hand it over to the installation technician. Technicians will pack these accessories into a whole machine and deliver them to the customer.

No matter what the computer is, it is always composed of CPU, memory, motherboard, hard disk, graphics card and other components, and the installation process is always fixed:

L fix the motherboard in the chassis

L install the CPU on the motherboard

L install the memory on the motherboard

L connect the hard disk to the motherboard

L install the video card on the motherboard

However, the components of each compatible machine are different. Some configurations are higher and some are lower, which is a change point. For the installation technician, he does not need to consider where these accessories come from. He just needs to assemble them together. This is a stable installation process. To separate the changed accessories from the stable process, we need to introduce the builder mode.

 

ExampleCode

 

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. text;

UsingSystem. reflection;

 

NamespaceBuilderexemple

{

Classprogram

{

StaticvoidMain (String[] ARGs)

{

ComputerfactoryFactory =Newcomputerfactory();

ComputerbuilderOffice =Newofficecomputerbuilder();

Factory. buildcomputer (office );

Office. Computer. showsysteminfo ();

ComputerbuilderGame =Newgamecomputerbuilder();

Factory. buildcomputer (game );

Game. Computer. showsysteminfo ();

}

}

 

Classcomputerfactory

{

PublicvoidBuildcomputer (ComputerbuilderCB)

{

Console. writeline ();

Console. writeline (">>>>>>>>>>>>>>>>>> start building"+ CB. Name );

CB. setupmainboard ();

CB. setupcpu ();

CB. setupmemory ();

CB. setupharddisk ();

CB. setupvideocard ();

Console. writeline (">>>>>>>>>>>>>>>>>> build"+ CB. Name +"Completed");

Console. writeline ();

}

}

 

Abstractclasscomputerbuilder

{

ProtectedstringName;

 

PublicstringName

{

Get{ReturnName ;}

Set{Name =Value;}

}

 

ProtectedcomputerComputer;

 

PubliccomputerComputer

{

Get{ReturnComputer ;}

Set{Computer =Value;}

}

 

PublicComputerbuilder ()

{

Computer =Newcomputer();

}

 

PublicpolicactvoidSetupmainboard ();

PublicpolicactvoidSetupcpu ();

PublicpolicactvoidSetupmemory ();

PublicpolicactvoidSetupharddisk ();

PublicpolicactvoidSetupvideocard ();

}

 

Classofficecomputerbuilder:Computerbuilder

{

PublicOfficecomputerbuilder ()

{

Name ="Officecomputer";

}

 

PublicoverrisponidSetupmainboard ()

{

Computer. mainboard ="Abit upgrade LG-95C motherboard (Intel 945gc chipset/LGA 775/1066 MHz )";

}

 

PublicoverrisponidSetupcpu ()

{

Computer. CPU ="Intel saiyang D 336 (2.8 GHz/LGA 775/256 k/533 MHz )";

}

 

PublicoverrisponidSetupmemory ()

{

Computer. Memory ="Patriot Bodi DDR2 667 512 MB desktop memory";

}

 

PublicoverrisponidSetupharddisk ()

{

Computer. harddisk ="Hitachi sataii interface desktop hard drive (80g/7200 RPM/8 m) boxed";

}

 

PublicoverrisponidSetupvideocard ()

{

Computer. videocard ="Motherboard integration";

}

}

 

Classgamecomputerbuilder:Computerbuilder

{

PublicGamecomputerbuilder ()

{

Name ="Gamecomputer";

}

 

PublicoverrisponidSetupmainboard ()

{

Computer. mainboard ="Gigabyte GA-965P-DS3 3.3 motherboard (Intel p965 Dongguan )";

}

 

PublicoverrisponidSetupcpu ()

{

Computer. CPU ="Intel Core e4400 (2.0 GHz/LGA 775/2 m/800 MHz) boxed";

}

 

PublicoverrisponidSetupmemory ()

{

Computer. Memory ="G. Skill Zhiqi F2-6400CL5D-2GBNQ DDR2 800 1g * 2 desktop memory";

}

 

PublicoverrisponidSetupharddisk ()

{

Computer. harddisk ="Hitachi sataii interface desktop hard drive (250 GB/7200 RPM/8 m) boxed";

}

 

PublicoverrisponidSetupvideocard ()

{

Computer. videocard ="Rainbow Yi color GT-GD3 up flame god of war H10 graphics card (geforce 8600gt/256 m/ddr3) Support HDMI! ";

}

}

 

Classcomputer

{

PrivatestringVideocard;

 

PublicstringVideocard

{

Get{ReturnVideocard ;}

Set{Videocard =Value;}

}

 

PrivatestringCPU;

 

PublicstringCPU

{

Get{ReturnCPU ;}

Set{CPU =Value;}

}

 

PrivatestringMainboard;

 

PublicstringMainboard

{

Get{ReturnMainboard ;}

Set{Mainboard =Value;}

}

 

PrivatestringMemory;

 

PublicstringMemory

{

Get{ReturnMemory ;}

Set{Memory =Value;}

}

 

PrivatestringHarddisk;

 

PublicstringHarddisk

{

Get{ReturnHarddisk ;}

Set{Harddisk =Value;}

}

 

PublicvoidShowsysteminfo ()

{

Console. writeline ("========================== systeminfo =============================");

Console. writeline ("CPU :"+ CPU );

Console. writeline ("mainboard :"+ Mainboard );

Console. writeline ("memory :"+ Memory );

Console. writeline ("videocard :"+ Videocard );

Console. writeline ("harddisk :"+ Harddisk );

}

}

}

The code execution result is as follows:

 

 

Code Description

 

L computerfactory is the mentor of the builder mode. The mentor is doing stable construction work. Assuming it is a technician, he is only doing repetitive work to assemble accessories into computers according to a fixed process. He does not know whether he is assembling a game computer or an office computer. He does not know whether the memory he has installed on the motherboard is 1 GB or 2 GB. He seems to be an incompetent technician.

L computerbuilder is an abstract builder role. It is mainly used to define two interfaces. One interface is used to regulate the composition of each part of the product. For example, five processes are required to assemble a computer. The second interface is used to return the constructed product. Here we do not define an abstract method. The computer is always built.

L officecomputerbuilder and gamecomputerbuilder are specific builders. His job is to implement the interfaces of various construction steps and the interfaces that implement the returned products. The latter is omitted here.

L computer is a complex product. In the Code, all of our construction steps are for the creation of various accessory services in the product, the computer defines a relatively specific product, in applications, this product can be highly abstracted so that different specific builders can even build completely different products.

L look at the client code. The user first chooses a specific builder. the user should be clear whether it needs a game computer or an office computer, but it can know nothing about the computer, A reasonable configuration ticket is provided by the sales staff. Then the user asked computerfactory to assemble the computer for it. After the Assembly is complete, computerfactory is started and the user is checked whether the computer configuration is correct.

L you may think that computerbuilder is similar to the abstract factory role in the abstract factory model, and gamecomputerbuilder is like a specific factory. In fact, the builder mode and the abstract factory mode have different focuses. The former emphasizes the concept of an Assembly. A complex object is composed of multiple parts and assembled in a certain standard sequence, the latter emphasizes creating a series of products. The builder model is applicable to assembling a computer, while the abstract factory model is applicable to the product series that provide user laptops, desktops, and handheld computers.

 

When to use

 

L from the code point of view, if you want to separate complex types of building rules and internal components, or want to use the same building process to build different types, you can consider using the builder mode.

L from the application perspective, if you want to decouple the product creation process and specific accessories, or you can consider using the builder mode when you want to create and reuse a stable and complex logic for all products.

 

Implementation points

 

L The object construction process is completed by the instructor. The specific composition is completed by the specific builder, indicating that the object is separated from the building.

L The builder and mentor are the key points of the builder mode. If it is merged or omitted, it may be changed to the template method mode.

L if the object construction steps are simple and the product has consistent interfaces, you can switch to the factory mode.

 

Notes

 

L whether the return method of the product is required or not, and whether there must be an interface in the abstract builder depends on the actual situation. If they have a unified interface, they can reflect this abstract method in the abstract builder. If there is no unified interface (for example, production of unrelated products) you can implement this method in specific builders. If the created product is a product, you can even omit the interface returned to the product (this is the example in this article ).

Link: http://www.cnblogs.com/lovecherry/archive/2007/10/07/915986.html

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.