Big talk design model Reading Notes 11 -- Builder Model)

Source: Internet
Author: User

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

1. What is Builder?

Is the abstract interface specified by each part of a Product object.

2. What is ConcreteBuilder?

It is a specific Builder that implements the Builder interface and constructs and assembles various components.

3. What is Director?

Build an object using the Builder interface.

Builder mode code Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace Builder
{
Class Program
{
Static void Main (string [] args)
{
Director dire= new director ();
Builder builder = new ConcreteBuilder ();
Director. Construct (builder );
Product p1 = builder. GetResult ();
P1.Show ();
Console. ReadLine ();
}
}
Abstract class Builder
{
Public abstract void BuilderA ();
Public abstract void BuilderB ();
Public abstract Product GetResult ();
}
Class Product
{
IList <string> parts = new List <string> ();
Public void Add (string part)
{
Parts. Add (part );
}
Public void Show ()
{
Console. WriteLine ("product creation ------");
Foreach (string part in parts)
{
Console. WriteLine (part );
}
}
}
Class ConcreteBuilder: Builder
{
Private Product product = new Product ();
Public override void BuilderA ()
{
Product. Add ("Part ");
}
Public override void BuilderB ()
{
Product. Add ("Part B ");
}
Public override Product GetResult ()
{
Return product;
}
}
Class Director
{
Public void Construct (Builder builder)
{
Builder. BuilderA ();
Builder. BuilderB ();
}
}
}

The builder mode is used when the algorithms for creating complex objects are independent of the components of the objects and their assembly methods.

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.