Design Patterns learning Summary 2-creation 2-Builder patterns

Source: Internet
Author: User

Builder mode (Creation type)

Purpose:

The builder mode separates the process of generating complex objects from its constructor. The same constructor can instance different objects.

Role
The builder pattern separates the specification of a complex object from its actual construction. The same construction process can create different representations.

Design:

The Generator interface ibuilder defines the content to be built.
Instance Object controller Director, including a series of object generation operations
Builder, called by the instantiated object controller director to instantiate an object
Object product, which consists of multiple parts

Example:
Specifications for the manufacture of pens by ibuilder
Director uses buildera to create a pen
Buildera method of creating pens according to ibuilder specifications
A pen composed of multiple parts.
Code:

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace Builder
{
Using system;
Using system. Collections. Generic;
Using System. Text;
Using System. Reflection;

Namespace BuilderExemple
{
Class Program
{
Static void Main (string [] args)
{
ComputerFactory factory = new ComputerFactory ();
ComputerBuilder office = new OfficeComputerBuilder ();
Factory. BuildComputer (office );
Office. Computer. ShowSystemInfo ();
ComputerBuilder game = new GameComputerBuilder ();
Factory. BuildComputer (game );
Game. Computer. ShowSystemInfo ();
}
}

Class ComputerFactory
{
Public void BuildComputer (ComputerBuilder cb)
{
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 ();
}
}

Abstract class ComputerBuilder
{
Protected string name;

Public string Name
{
Get {return name ;}
Set {name = value ;}
}

Protected computer;

Public Computer
{
Get {return computer ;}
Set {computer = value ;}
}

Public ComputerBuilder ()
{
Computer = new Computer ();
}

Public abstract void SetupMainboard ();
Public abstract void SetupCpu ();
Public abstract void SetupMemory ();
Public abstract void SetupHarddisk ();
Public abstract void SetupVideocard ();
}

Class OfficeComputerBuilder: ComputerBuilder
{
Public OfficeComputerBuilder ()
{
Name = "OfficeComputer ";
}

Public override void SetupMainboard ()
{
Computer. Mainboard = "Abit upgrade LG-95C motherboard (Intel 945GC chipset/LGA 775/1066 MHz )";
}

Public override void SetupCpu ()
{
Computer. Cpu = "Intel saiyang D 336 (2.8 GHz/LGA 775/256 K/533 MHz )";
}

Public override void SetupMemory ()
{
Computer. Memory = "Patriot Bodi DDR2 667 512 MB desktop Memory ";
}

Public override void SetupHarddisk ()
{
Computer. Harddisk = "Hitachi SATAII interface desktop hard drive (80G/7200 RPM/8 M) boxed ";
}

Public override void SetupVideocard ()
{
Computer. Videocard = "motherboard integration ";
}
}

Class GameComputerBuilder: ComputerBuilder
{
Public GameComputerBuilder ()
{
Name = "GameComputer ";
}

Public override void SetupMainboard ()
{
Computer. Mainboard = "GIGABYTE GA-965P-DS3 3.3 motherboard (INTEL P965 Dongguan )";
}

Public override void SetupCpu ()
{
Computer. Cpu = "Intel Core 2 E4400 (2.0 GHz/LGA 775/2 M/800 MHz) boxed ";
}

Public override void SetupMemory ()
{
Computer. Memory = "G. SKILL Zhiqi F2-6400CL5D-2GBNQ DDR2 800 1G * 2 desktop Memory ";
}

Public override void SetupHarddisk ()
{
Computer. Harddisk = "Hitachi SATAII interface desktop hard drive (250 GB/7200 RPM/8 M) boxed ";
}

Public override void SetupVideocard ()
{
Computer. Videocard = "colorful 8600GT-GD3 UP flame Ares H10 graphics card (GeForce 8600GT/256 M/DDR3) supports HDMI! ";
}
}

Class Computer
{
Private string videocard;

Public string Videocard
{
Get {return videocard ;}
Set {videocard = value ;}
}

Private string cpu;

Public string Cpu
{
Get {return cpu ;}
Set {cpu = value ;}
}

Private string mainboard;

Public String mainboard
{
Get {return mainboard ;}
Set {mainboard = value ;}
}

Private string memory;

Public String memory
{
Get {return memory ;}
Set {memory = value ;}
}

Private string harddisk;

Public string Harddisk
{
Get {return harddisk ;}
Set {harddisk = value ;}
}

Public void ShowSystemInfo ()
{
Console. writeLine ("========================== SystemInfo ============================= ");
Console. WriteLine ("CPU:" + cpu );
Console. WriteLine ("MainBoard:" + mainboard );
Console. WriteLine ("Memory:" + memory );
Console. WriteLine ("VideoCard:" + videocard );
Console. WriteLine ("HardDisk:" + harddisk );
Console. ReadLine ();
}
}
}
}

 

Use Cases:

The algorithms used to create various components of an object can be separated;
The combined objects may have different performances.
Need to control the Object Construction Process

Use the Builder pattern when...
• The algorithm for creating parts is independent from the parts themselves.
• The object to be assembled might have different representations.
• You need fine control over the construction process.

 

Summary:

The Builder mode is a creation mode, which is mainly used to create complex objects in a project. The so-called "complex object" means that this object also contains other sub-objects. Scenarios of the Builder mode: Sub-objects of various parts of a complex object can be composed by certain algorithms. due to changes in requirements, each part of the complex object is often subject to drastic changes, however, the algorithms that combine sub-objects in each part are relatively stable. To put it simply, sub-objects change frequently and are relatively stable for the combined sub-object algorithm.
This is a solution to the creation of a complex object. The changed part and the relatively stable part have been clarified. What we need to do is to isolate the change and how to isolate the sub-objects from the algorithm is a problem to be solved.
In design patterns, the construction of a complex object is separated from its representation, so that the same construction process can create different representations. Key points of the Builder mode:
1. It is used to construct a complex object, but the algorithm for constructing this object is stable, and each part of the object changes frequently.
2. Respond to frequent changes in requirements of various parts of complex objects. However, it is difficult to cope with changes in algorithm requirements. Note that if the combination algorithm of internal sub-objects of complex objects is unstable, using Builder will cause a lot of unnecessary trouble.

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.