Builder Pattern)

Source: Internet
Author: User

Today, we re-read the builder mode [Build Pattern] And finally understood its intention and application scenarios.
It is intended to create a complex object, which consists of a series of stable actions. The specific actions of a single product are different. The focus is to separate a complex build from its representation so that the same build process can create different representations.

The process of abstraction and encapsulation is very similar to the template method mode, but note that their focus is different: the template method focuses on "behavior" and refers to the implementation of a series of stable actions, it belongs to the behavior mode, while the builder mode is intended to create an object that is complex and has multiple implementation steps. It belongs to the creation mode.

The following code is used to assemble a computer. A computer can be seen as a complex object. The assembly process requires the installation of cpu, motherboard, memory, hard disk, and other components. Of course, the components installed on different brands of computers are different, but each step in this process is indispensable and stable.

Structure:

 

Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace BuildPattern
{
Class Program
{
Static void Main (string [] args)
{
Computer pc = new MsComputer ();
Build build = new Build (pc );
Build. BeginBuildComputer ();

Pc. ShowMyComputer ();
}
}

// Product abstract class
Abstract class Computer
{
Public string Cpu {set; get ;}

Public string HardDisk {set; get ;}

Public string KeyBoard {set; get ;}

Public string Memory {set; get ;}

Public abstract void SetCpu ();

Public abstract void SetHardDisk ();

Public abstract void SetKeyBoard ();

Public abstract void SetMemory ();

Public void ShowMyComputer ()
{
Console. WriteLine ("------------");
Console. WriteLine ("My Compter Config :");
Console. WriteLine ("Cpu:" + Cpu );
Console. WriteLine ("KeyBoard:" + KeyBoard );
Console. WriteLine ("HardDisk:" + HardDisk );
Console. WriteLine ("Memory:" + Memory );
}
}

// Specific class
Class InterComputer: Computer
{
Public override void SetCpu ()
{
Console. WriteLine ("setting Inter Cpu for this computer ");
Cpu = "Inter Cpu ";
}

Public override void SetHardDisk ()
{
Console. WriteLine ("setting Inter HardDisk for this computer ");
HardDisk = "Inter HardDisk ";
}

Public override void SetKeyBoard ()
{
Console. WriteLine ("setting Inter KeyBoard for this computer ");
KeyBoard = "Inter KeyBoard ";
}

Public override void SetMemory ()
{
Console. WriteLine ("setting Inter Memory for this computer ");
Memory = "Inter Memory ";
}
}

// Specific class
Class MsComputer: Computer
{
Public override void SetCpu ()
{
Console. WriteLine ("setting MS Cpu for this computer ");
Cpu = "Ms Cpu ";
}

Public override void SetHardDisk ()
{
Console. WriteLine ("setting MS HardDisk for this computer ");
HardDisk = "Ms HardDisk ";
}

Public override void SetKeyBoard ()
{
Console. WriteLine ("setting MS KeyBoard for this computer ");
KeyBoard = "Ms KeyBoard ";
}

Public override void SetMemory ()
{
Console. WriteLine ("setting MS Memory for this computer ");
Memory = "Ms Memory ";
}
}

// Builder
Class Build
{
Computer computer;
Public Build (Computer computer)
{
This. computer = computer;
}

Public void BeginBuildComputer ()
{
Computer. SetCpu ();
Computer. SetHardDisk ();
Computer. SetKeyBoard ();
Computer. SetMemory ();
}
}
}

 

Learning reference:
Http://www.cnblogs.com/Terrylee/archive/2005/12/19/299878.html

Http://www.cnblogs.com/zhenyulu/articles/37378.aspx

The builder mode of the big talk Design Model

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.