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 partAlgorithmComposition: due to changes in requirements, various parts of this complex object often face 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?
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Structure:
Collaboration:
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 Builddoor (); // Portal
Public Abstract Void Buildwall (); // Wall
Public Abstract Void Buildwindows (); // Windows
Public Abstract Void Buildfloor (); // Floor
Public Abstract Void Buildhouseceiling (); // Roof
Public Abstract House gethouse (); // Build a house
}
Public Class Romainhouse: house {}
Public Class Romandoor: door {}
Public Class Romanwall: Wall {}
Public Class Romanwindows: Windows {}
Public Class Romanfloor: Floor {}
Public Class Romanhouseceiling: houseceiling {}
Public Class Romainhousebuilder: Builder
{
Public Override Void Builddoor (){}
Public Override Void Buildwall (){}
Public Override Void Buildwindows (){}
Public Override Void Buildfloor (){}
Public Override Void Buildhouseceiling (){}
Public Override House gethouse ()
{
Return New Romainhouse ();
}
}
Public Class Gamemanager
{
Public Static House createhouse (Builder)
{
Builder. buildwall (); // Create the first wall
Builder. buildwall (); // Create a second wall
Builder. buildwall (); // Create a third Wall
Builder. buildwall (); // Create the fourth wall
Builder. buildwindows (); // Create the first window
Builder. buildwindows (); // Create the second window
Builder. builddoor ();
Builder. buildfloor ();
Builder. buildhouseceiling ();
Return Builder. gethouse ();
}
}
Class App
{
Public Static Void Main ()
{
// House = gamemanager. createhouse (New romainhousebuilder ());
String Assemblyname = System. configuration. configurationsettings. deleettings [ " Builderassembly " ];
String Buildername = System. configuration. configurationsettings. deleettings [ " Builderclass " ];
System. reflection. Assembly = System. reflection. Assembly. Load (assemblyname );
Type T = Assembly. GetType (buildername );
Builder = (Builder) activator. createinstance (t );
House house = Gamemanager. createhouse (builder );
}
}
Key points:
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 demand changes of "series objects" and the builder mode solves the demand changes of "Object parts. The builder mode is usually used in combination with the composite mode.
Q &:
1. The buildpart method in builder mode is a virtual method. Methods in builder mode can return interfaces or abstract classes;
2. The Builder mode mainly aims to cope with changes in the demand for "sub-objects". Its disadvantage is that it is difficult to cope with changes in the demand for "step-by-step building algorithms;