Builder mode

xiaoxiao2021-03-06  46

When you see the Builder mode, you feel familiar, and the abstract factory model will be very similar to our abstract factory, I think the same model is used with two different names, so I have some sparks below.

Builder mode definition: Separate a complex object with its representation, so that the same build process can create different representations

This translation is very vague, looked, but this is the most translated online, so I don't want to understand him, anyway, I hope to translate this sentence all the way. So those translations foreign People in the article must pay attention, if you are not understanding, don't turn out, harm everyone.

The following uses the original text:

The Builder pattern allows a client object to construct a complex object by specifying only its type and content The client is shielded from the details of the object's construction.. (Brief: hides the details of object construction)

It is a pattern for step-by-step creation of a complex object so that the same construction process can create different representations is the routine in the builder pattern that also makes for finer control over the construction process. All the different builders generally inherit from .

Builder has a similar motivation to the abstract factory but, whereas in that pattern, the client uses the abstract factory class methods to create its own object, in Builder the client instructs the builder class on how to create the object and then asks it for the RESULT. How the class is put together is up to the builder class. It's a subtle difference.

The Builder pattern is applicable when the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled and the construction process must allow different representations for the object that's constructed

EXAMPLE

Below is an example of creating a House, the clients asks the Director (CDirector class) to create a House by calling BuildHouse method which takes a boolean parameter (blnBackyard), the director then creates an Apartment (Concrete Builder) if the blnBackyard is false examples or a Single Family Home (Concrete Builder) if the blnBackyard is true (both of them implements iHouse interface) and returns iHouse (Abstract Builder) interface. (the following example is a construction of their houses, customers with buildhouse (Boolean blnbackyard) Building a house, The Director Does The Complex Building of a House and The Client Gets Back IHOUSE INTERFACE That Codes Against With Worrying About The Creation of House, Rooms, Backyard ETC.

C #

Using system; using system.collections;

Public interface IHOUSE // House interface {BOOL getBackyard (); // get a reference for the backyard Long noofrooms (); // House String discrtion (); // House Description}

Public class capt: 店 h 房 房 房 房 房 {;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Bedroom "; Rooms.Add (" Room1 ", ROOM);

Room = new croom (); room.roomname = "second bedroom"; rooms.add ("room2", room);

Room = new croom (); room.roomname = "Living room"; rooms.add ("room3", room); mblnbackyard = false; // Key, no backyard.

public bool GetBackyard () {return mblnBackyard;} public long NoOfRooms () {return Rooms.Count;} public string Discription () {IDictionaryEnumerator myEnumerator = Rooms.GetEnumerator (); string strDiscription; strDiscription = "This is an Apartment with" Rooms.Count "Rooms / n"; strDiscription = strDiscription "This Apartment does not have a backyard / n"; while (myEnumerator.MoveNext ()) {strDiscription = strDiscription "/ n" myEnumerator.Key " / t " ((CRoom) myEnumerator.Value) .RoomName;} return strDiscription;}} public class CSFH: iHouse {private bool mblnBackyard; private Hashtable Rooms; public CSFH () {CRoom room; Rooms = new Hashtable ();

Room = new croom (); room.roomname = "Master bedroom"; rooms.add ("room1", room);

Room = new croom (); room.roomname = "second bedroom"; rooms.add ("room2", room);

Room = new croom (); room.roomname = "third room"; rooms.add ("room3", room); room.roomname = "living room"; rooms.add ("room4" , ROOM);

Room = new croom (); room.roomname = "guest room"; rooms.add ("room5", room);

Mblnbackyard = true; // has a backyard}

public bool GetBackyard () {return mblnBackyard;} public long NoOfRooms () {return Rooms.Count;} public string Discription () {IDictionaryEnumerator myEnumerator = Rooms.GetEnumerator (); string strDiscription; strDiscription = "This is an Single Family Home with " Rooms.count " Rooms / N "; strdiscription = strdiscription " this house has a backyard / n "; while (myenumerator.movenext ()) {strdiscription = strdiscription " / n " myenumerator.key " / T " (croom) Myenumerator.value ).Roomname;} Return strdiscription;}} public interface Iroom // Room interface. {String Roomname {get; set;}}

Public class crun: Iroom {private string mstrroomname; public string roomname {get {rrn mstrroomname;} set {mstrroomname = value;}}}

Public class cdirector {public iHouse buildhouse // Returns different houses. Builder, Director to overall control to use Builder to create. {if (blnbackyard) {Return new csfh (); // Product house } Else {return new capt (); // Do not have a housing}}}}}}}

public class Client {static void Main (string [] args) {CDirector objDirector = new CDirector (); IHouse objHouse; objHouse = objDirector.BuildHouse (bool.Parse (args [0])); Console.WriteLine (objHouse.Discription ( );

转载请注明原文地址:https://www.9cbs.com/read-81269.html

New Post(0)