Creational Pattern - Builder

zhaozj2021-02-11  181

Creational Pattern - Builder

Authordate of SubmissionUser Levelashish Jaiman02 / 19 / 2002Beginner

Reference: E. Gamma et al., "Design Patterns: Elements of Reusable Object-Oriented Software" ISBN 0-201-63361-2, Addison Wesley, 1995. Source Code: CreationalPatterBuildercode.zip 2 KB

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 Zha 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 an abstract builder class that declares the general functions to be used by the director to let the builder create the product in parts. 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 THE CLAS PUT TOGETHER IS UP TO The Builder Class. It's a subtle Difference. The Builder Pattern is Applicab le 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 Zha 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 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 director does the complex building of a House and the client gets back iHouse interface that it codes Against WithWorrying About The Creation of House, Rooms, Backyard etc. C # ImplementationUsing System; Using System.collections;

Public interface IHOUSE {BOOL getBackyard (); long noofrooms (); string discrtiption ();

public class CApt: IHouse {private bool mblnBackyard; private Hashtable Rooms; public CApt () {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 = "Living room"; rooms.add ("room3", room); mblnbackyard = false;}

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;

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) .roomAme;} returnason} Return StrDiscription;}}

Public interface iroom {string roomname {get; set;}}

public class CRoom: IRoom {private string mstrRoomName; public string RoomName {get {return mstrRoomName;} set {mstrRoomName = value;}}} public class CDirector {public IHouse BuildHouse (bool blnBackyard) {if (blnBackyard) {return new CSFH ( } Else {return new capt ();}}}

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-4376.html

New Post(0)