Definition of builder pattern:
Separating the construction of a complex object from its representation, so that the same build process can create different representations, such design patterns are called builder patterns
Builder Pattern Chart:
Builder Pattern Role:
1 Builder: Specifies an abstract interface for each part that creates a product object.
2 ConcreteBuilder: Implements the builder interface to construct and assemble the parts of the product, define and identify the representations it creates, and provide an interface to retrieve the product.
3 Director: Constructs an object that uses the builder interface.
4 Product: Represents a constructed complex object. ConcreteBuilder creates an internal representation of the product and defines its assembly process, containing the classes that define the components, including the interfaces that assemble the parts into the final product.
The following is an example of a real-life building to explain the builder model:
1. Abstract the builder's interface, the conditions within which the building is to be implemented, the number of rooms to be created after the creation, and the description of the house.
<summary>
///Abstract builder
///</summary> public
interface Ihouse
{
///<summary>
///Create the conditions of the house kind
///</summary>
///<returns></returns>
bool Getbackyard ();
<summary>
number of rooms created by///
///</summary>
///<returns></returns>
Long Noofrooms ();
<summary>
///Description
///</summary>
///<returns></returns>
string Description ();
}
2. Inherit Ihouse interface, concrete builder, here created a room, which includes living room, kitchen, toilet, bedroom, a total of four rooms such a house.
public class Croom {public string Roomname {get; set;}
}///<summary>///specific builder///</summary> public class Csfh:ihouse {private bool Mblnbackyard;
Private Hashtable rooms;
Public Csfh () {Croom room = new Croom (); Room.
Roomname = "First floor living room";
Rooms = new Hashtable ();
Rooms.add ("Room1", room);
room = new Croom (); Room.
Roomname = "First floor kitchen";
Rooms.add ("Room2", room);
room = new Croom (); Room.
Roomname = "First floor toilet";
Rooms.add ("Room3", room);
room = new Croom (); Room.
Roomname = "First floor bedroom";
Rooms.add ("room4", room);
Mblnbackyard = true;
public bool Getbackyard () {return mblnbackyard;
Public long Noofrooms () {return rooms.count;
public string Description () {IDictionaryEnumerator myenumerator = Rooms.getenumerator ();
String strdescription = "This House altogether" + Rooms.count + "between \ n"; while (Myenumerator.movenext ()) {strdescription = strdescription + "\ n" + Myenumerator.key + "\ T" + ((Croom) Myenumerato R.value).
Roomname; Return Strdescription;
}
}
3. Inherit Ihouse interface, concrete builder, here created a house, which includes only bedroom, living room, kitchen a total of three rooms such a house.
<summary>
///Other concrete builders
///</summary> Public
class Capt:ihouse
{
private bool Mblnbackyard;
Private Hashtable rooms;
Public CApt ()
{
rooms = new Hashtable ();
Croom room = new Croom ();
Room. Roomname = "bedroom";
Rooms.add ("Room1", room);
room = new Croom ();
Room. Roomname = "living room";
Rooms.add ("Room2", room);
room = new Croom ();
Room. Roomname = "Kitchen";
Rooms.add ("Room3", room);
Mblnbackyard = false;
}
public bool Getbackyard ()
{return
mblnbackyard;
}
Public long Noofrooms () {return
rooms.count;
}
public string Description () {
IDictionaryEnumerator myenumerator = Rooms.getenumerator ();
String strdescription = "This House altogether" + Rooms.count + "between \ n";
while (Myenumerator.movenext ())
{
strdescription = strdescription + "\ n" + Myenumerator.key + "\ T" + ((Croom) my Enumerator.value). roomname;
}
Return strdescription
}
}
4. Create mentors to instruct which builders are required to build a room.
<summary>
///Mentor
///</summary> Public
class Cdirector
{public
Ihouse Buildhouse (bool Blnbackyard)
{
if (Blnbackyard)
{return
new CSFH ();
}
else
{return
new CApt ();
}
}
}
5. Create:
static void Main (string[] args)
{
Cdirector objdirector = new Cdirector ();//instantiation of the Mentor
Ihouse Objhouse;
string input = Console.ReadLine ()//input condition instructs which creator to create the room
Objhouse = Objdirector.buildhouse (bool. Parse (Input));
Console.WriteLine (Objhouse.description ());
Console.ReadLine ();
}
The builder pattern is primarily used to "step through a complex object", where "step-by" is a stable algorithm, while parts of complex objects often change
The product does not require an abstract class, especially if the algorithm for creating the object is complex and results in the use of this pattern, or if the pattern is applied to the production process of the product, the end result may vary widely and is unlikely to extract an abstract product class.
The previous abstract factory pattern solves the demand change of "series objects", and the Builder mode solves the requirement change of "object part".
The use of the builder model allows the internal representation of the product to change independently. Using builder mode allows the client to not know the details of the internal composition of the product
Each builder is relatively independent of the other builder.
The builder pattern applies to the attribute interdependence of the product objects that need to be generated, and the builder pattern can be forced to generate order. The product object that needs to be generated has a complex internal structure.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.