Origin of builder Mode
- Suppose you create a house facility in the game. The building of this House consists of several parts and each part must be changeable.
- 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)
- 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.
- 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
- 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.
- 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.
- 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.