Builder mode (Creation Mode)

Source: Internet
Author: User

Origin of builder Mode

    1. Suppose you create a house facility in the game. The building of this House consists of several parts and each part must be changeable.
    2. If the most intuitive design method is used, changes in each part of the house will lead to the re-correction of the house building ......

Motivation)

    1. In a software system, sometimes it is faced with the creation of "a complex object", which is usually caused by the sub-objects of each partAlgorithmComponents; due to changes in requirements, each part of this complex object is often subject to drastic changes, but the algorithms that combine them are relatively stable.
    2. How to deal with this change? How can we provide a "encapsulation mechanism" to isolate the changes of "various parts of complex objects", so as to keep the "stable Construction Algorithm" in the system from changing with demand?

Intent)
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. -- Design Pattern gof

Structure)

 

Collaborations)

 

Builder application in game framework

Code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

UsingSystem. configuration;
UsingSystem. reflection;

Namespace Testwpf
{
Public   Abstract   Class House {}
Public   Abstract   Class Door {}
Public   Abstract   Class Wall {}
Public   Abstract   Class Windows {}
Public   Abstract   Class Floor {}
Public   Abstract   Class Houseceiling {}

Public   Abstract   Class Builder
{
Public   Abstract   Void Buildingdoor ();
Public   Abstract   Void Buildingwall ();
Public   Abstract   Void Buildingwindows ();
Public   Abstract   Void Buildingfloor ();
Public   Abstract   Void Buildinghouseceiling (); // Roof

Public   Abstract House gethouse ();
}

Public   Class Gamemanager
{
///   <Summary>
/// Stable Construction Algorithm
///   </Summary>
Public   Static House createhouse (Builder)
{
Builder. buildingdoor ();
Builder. buildingdoor ();

Builder. buildingwall ();
Builder. buildingwall ();

Builder. buildingwindows ();
Builder. buildingwindows ();

Builder. buildingfloor ();
Builder. buildinghouseceiling ();

ReturnBuilder. gethouse ();

}
}

# RegionHouses of different styles
Public ClassRomainhouse: House
{
}

Public ClassRomaindoor: Door
{
}

Public ClassRomainwall: Wall
{
}

Public ClassRomainwindows: Windows
{
}

Public ClassRomainfloor: Floor
{
}

Public ClassRomainhouseceiling: houseceiling
{
}

Public ClassRomainhouse: House
{
}

Public   Class Romainhousebuilder: Builder
{
Public   Override   Void Buildingdoor ()
{

}

Public   Override   Void Buildingwall ()
{

}

Public   Override   Void Buildingwindows ()
{
}

Public   Override   Void Buildingfloor ()
{
}

Public   Override   Void Buildinghouseceiling ()
{
}

Public OverrideHouse gethouse ()
{
Return NewRomainhouse ();
}
}
# Endregion

# Region Client
Public   Class App
{
Public   Static   Void Main ()
{
# Region General Usage
// House otherhouse = gamemanager. createhouse (New otherhousebuilder ()); // Other house styles
House house = Gamemanager. createhouse ( New Romainhousebuilder ()); // Rome-style house
# Endregion

# Region Use the. NET launch mechanism to dynamically build
String Assemblyname = Configurationsettings. deleettings [ " Builderassembly " ];
String Buildername = Configurationsettings. deleettings [ " Buildingerclass " ];

Assembly = assembly. load (assemblyname);
type T = assembly. getType (buildername);
builder = (builder) activator. createinstance (t);

House Hhouse=Gamemanager. createhouse (builder );
# Endregion

}
}

# Endregion

}

 

 

 

Key Points of builder Mode

    1. the builder mode is mainly used to "build a complex object by step ". In this process, "step-by-step" is a stable algorithm, while all parts of complex objects change frequently.
    2. where is the change point, and where is the encapsulation? The Builder mode is mainly used to cope with frequent changes in the "various parts of complex objects. Its disadvantage is that it is difficult to cope with changes in the demand for "step-by-step algorithm building.
    3. the abstract factory mode solves the changes in the demand for "series objects" and the builder mode solves the changes in the demand for "Object parts. The builder mode is usually used in combination with the composite 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.