Introduction
This article discusses the generator design mode and how to implement the mode when it is used. And. Finally, there will be a simple generator mode implementation.
Background
When ourProgramCreate an object. This object must be constructed by many different objects. To construct the final object. We have to combine some objects. Finally, we will find ourCodeIt is hard to understand the details of various objects.
To illustrate the above situation. Here is an example of a mobile phone manufacturing system. Suppose we have a system that has been installed on the mobile phone supplier. Currently, suppliers need to create a new mobile phone based on some parameters. Such as touch screen, operating system, and battery. If we already have objects in these parts, any combination of the above parts will make the client code complicated and difficult to manage. For example, the module that determines the type of mobile phone to be produced.
The generator mode aims to solve the above problems. Gof defines the generator mode as follows:
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
This definition means we have to design this system. A client only defines parameters, while a generator takes over the method of creating complex objects. Let's take a look at the class diagram of the generator mode.
Then let's see what each class represents.
Concretebuilder: create a specific class for a complex product. You will know the product he has created, that is, the product he has assembled. The client obtains the product object through this class.
Builder: interfaces for creating products
Director: client code that defines which parts should be combined to create a specific product
Product: This is an object created by combining multiple parts
Use Code
Now we follow the above definition and try to implement a basic generator mode.
First, let's define different parts of the product in a proper place, and simply define some enumeration types. Then we can create a product by combining different parts.
//Some helper enumerations define various partsPublic EnumScreentype {screentype_touch_capacitive, screentype_touch_resistive, screentype_non_touch };Public EnumBattery {mah_1000, mah_1500, mah_2000 };Public EnumOperatingsystem {Android, windows_mobile, windows_phone, Symbian };Public EnumStylus {yes, no };
Next, let's take a look at the product class. We need a product class that can be created through Assembly. Here we define a mobilephone class, that is, the product class in the concept.
// This is a "product" Class Class Mobilephone { // Fields of different parts String Phonename; screentype phonescreen; battery phonebattery; operatingsystem phoneos; stylus phonestylus; Public Mobilephone ( String Name) {phonename =Name ;} // Public attribute access to these parts Public String Phonename { Get { Return Phonename ;}} Public Screentype phonescreen { Get { Return Phonescreen ;} Set {Phonescreen = Value ;}} Public Battery phonebattery { Get { Return Phonebattery ;} Set {Phonebattery = Value ;}} Public Operatingsystem phoneos { Get { Return Phoneos ;} Set {Phoneos = Value ;}} Public Stylus phonestylus { Get { Return Phonestylus ;} Set {Phonestylus = Value ;}} // How to display mobile phone information Public Override String Tostring (){ Return String . Format ( " Name: {0} \ nscreen: {1} \ nbattery {2} \ Nos: {3} \ nstylus: {4} " , Phonename, phonescreen, phonebattery, phoneos, phonestylus );}}
Now that we have the product class, let's create the builder. The builder class should provide some methods for any part of the mobile phone. In this way, we create an iPhone builder, that is, the builder in the concept, and then read the code:
//This is a "Builder" ClassInterfaceIphonebuilder {VoidBuildscreen ();VoidBuildbattery ();VoidBuildos ();VoidBuildstylus (); mobilephone phone {Get;}}
Now our builder interface is ready. The next step is to create concretebuilder. We assume that the vendor requires an Android mobile phone and a WP mobile phone. We need two concretebuilder. Androidphonebuilder and windowsphonebuilder. We can specify the part types required by each mobile phone.
// This is the "concretebuilder" Class Class Androidphonebuilder: iphonebuilder {mobilephone; Public Androidphonebuilder () {Phone = New Mobilephone ( " Android phone " );} # Region Iphonebuilder members Public Void Buildscreen () {Phone. phonescreen = Screentype. screentype_touch_resistive ;} Public Void Buildbattery () {Phone. phonebattery = Battery. mah_1500 ;} Public Void Buildos () {Phone. phoneos = Operatingsystem. Android ;} Public Void Buildstylus () {Phone. phonestylus = Stylus. Yes ;} // Returns the created mobile phone object. Public Mobilephone phone { Get { Return Phone ;}} # Endregion }
// This is the "concretebuilder" Class Class Windowsphonebuilder: iphonebuilder {mobilephone; Public Windowsphonebuilder () {Phone = New Mobilephone ( " Windows Phone " );} # Region Iphonebuilder members Public Void Buildscreen () {Phone. phonescreen =Screentype. screentype_touch_capacitive ;} Public Void Buildbattery () {Phone. phonebattery = Battery. mah_2000 ;} Public Void Buildos () {Phone. phoneos = Operatingsystem. windows_phone ;} Public Void Buildstylus () {Phone. phonestylus =Stylus. No ;} // Returns the created mobile phone object. Public Mobilephone phone { Get { Return Phone ;}} # Endregion }
Finally, we will create the ctor class. The director class we created will have a composite function that accepts an iphonebuilder parameter and then calls the corresponding internal methods of their concretebuilder.
//This is the "director" ClassClassManufacturer {Public VoidConstruct (iphonebuilder phonebuilder) {phonebuilder. buildbattery (); phonebuilder. buildos (); phonebuilder. buildscreen (); phonebuilder. buildstylus ();}}
Now we have encapsulated the method for producing complex products in the standard generator mode. Now let's take a look at how simple it is to create a product on the client.
Class Program { Static Void Main ( String [] ARGs ){ // First create Director Manufacturer newmanufacturer = New Manufacturer (); // Then create the Builder Iphonebuilder phonebuilder = Null ; // Create an Android phone Phonebuilder =New Androidphonebuilder (); newmanufacturer. Construct (phonebuilder); console. writeline ( " A new phone built: \ n {0} " , Phonebuilder. Phone. tostring ()); // Create another WP mobile phone Phonebuilder = New Windowsphonebuilder (); newmanufacturer. Construct (phonebuilder); console. writeline ( " A new phone built: \ n {0} " , Phonebuilder. Phone. tostring ());}}
Now, if we want to create more objects, we only need a concretebuilder class. Other code will not be moved at all. Client code is also easier to create complex products. Let's look at the program output.
Finally, let's take a look at the class diagram of the program written in generator mode.
Download demo
Builder Pattern
License
This article includesSource codeAnd file authorization under cpol
Original article address: understanding-and-implementing-builder-pattern-in
Copyright statement: This article by http://leaver.me translation, welcome to repost share. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!