04-Mobile Package: Builder mode

Source: Internet
Author: User

4.1 Related background stories

Go to the Business Hall for mobile package, there are two kinds of packages can choose:

1) 20 Yuan package of 400 SMS packages;

2) 30 yuan package of 600 SMS packages.

And both types of packages must be set up to activate the ringtone business.

4.2 Pattern Definition

Builder pattern, in a software system, you may face the task of creating complex objects, and if we use a single method or a single object to create it will be cumbersome, when the complex objects created changes, the entire system may face drastic changes.

In order to solve this problem, we can decompose the creation process of this complex object into several parts, and each sub-part is composed of some algorithms. However, the sub-section may change frequently, how can the stability of the overall creation work be ensured? This requires the support of the builder model. The builder pattern separates the creation and presentation of complex objects so that the same build process can create different representations.

4.3 Pattern analysis in the story

4.3.1 Role Analysis

The objects that appear in the example are:

1) Customer Zhang San

2) Business operator

3) computers that set up customer plans

4) Mobile Package

Customer Zhang San is the end of the final mobile phone package, the business hall operator is the same as a mentor's identity, only the operator knows what the customer needs, and the computer as a builder role, the computer through the operation set up the customer's mobile phone package, mobile package is the final product, produced to the customer use.

The various roles in the example are listed below:

Customer Zhang San--end demand

Operator of the Business Hall-instructor (informs the builder what kind of mobile plan to produce)

Computer-Builders (build all kinds of mobile packages)

Mobile Package--Products

Static modeling of 4.3.2 Builder mode

  

, in the builder's factory will build mobile package object refinement decomposition into three processes: build the phone, build text messages, build ringtones. Each process realizes the function is specialized, single, three function does not interfere with each other, also has a function to return the mobile phone plan instance, causes the construction and the representation separation, the function module coupling degree greatly reduces, the module inside has the very high cohesion degree.

4.4 Mode Implementation

4.4.1 Create a product-Mobile package

 PackageCom.demo.builder.model;/*** Created by LSQ on 2018/3/14. * Mobile Package Category*/ Public classMobilepackage {//charges    Private floatMoney ; //SMS    Private intShortinfo; //Ringtones    PrivateString Music;  Public floatGetmoney () {returnMoney ; }     Public voidSetmoney (floatMoney ) {         This. Money =Money ; }     Public intGetshortinfo () {returnShortinfo; }     Public voidSetshortinfo (intshortinfo) {         This. Shortinfo =Shortinfo; }     PublicString Getmusic () {returnMusic; }     Public voidSetmusic (String music) { This. Music =Music; }}

4.4.2 Building Abstract Builders

1) Builder Interface--imobilebuilder

 PackageCOM.DEMO.BUILDER.ITF;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/14. * Mobile Package Builder*/ Public InterfaceImobilebuilder {//phone calls for building mobile packages     Public voidBuildmoney (); //SMS for building a mobile package     Public voidBuildshortinfo (); //Create ringtones for mobile packages     Public voidBuildmusic (); //back to the built mobile plan object     Publicmobilepackage getmobilepackage ();}

2) Abstract Builder--abstractbasepackage

 Packagecom.demo.builder.base;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/14. * Abstract Builders*/ Public Abstract classAbstractbasepackage {//Mobile Package Instance variables    protectedMobilepackage Mobilepackage;  PublicAbstractbasepackage () { This. Mobilepackage =NewMobilepackage (); }}

4.4.3 Build Concrete Builders

1) Concrete Builder--MOBILEBUILDERIMPL1

 PackageCOM.DEMO.BUILDER.ITF;ImportCom.demo.builder.base.AbstractBasePackage;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/14. * Package 1*/ Public classMobileBuilderImpl1extendsAbstractbasepackageImplementsImobilebuilder {//phone calls for building mobile packages     Public voidBuildmoney () { This. Mobilepackage.setmoney (20.0f); }    //SMS for building a mobile package     Public voidBuildshortinfo () { This. Mobilepackage.setshortinfo (400); }    //Create ringtones for mobile packages     Public voidBuildmusic () { This. Mobilepackage.setmusic ("We are all the same"); }    //back to the built mobile plan object     Publicmobilepackage getmobilepackage () {return  This. Mobilepackage; }}

2) Concrete Builder--MOBILEBUILDERIMPL2

 PackageCOM.DEMO.BUILDER.ITF;ImportCom.demo.builder.base.AbstractBasePackage;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/14. * Package 2*/ Public classMobileBuilderImpl2extendsAbstractbasepackageImplementsImobilebuilder {//phone calls for building mobile packages     Public voidBuildmoney () { This. Mobilepackage.setmoney (30.0f); }    //SMS for building a mobile package     Public voidBuildshortinfo () { This. Mobilepackage.setshortinfo (600); }    //Create ringtones for mobile packages     Public voidBuildmusic () { This. Mobilepackage.setmusic ("We're not the same"); }    //back to the built mobile plan object     Publicmobilepackage getmobilepackage () {return  This. Mobilepackage; }}

4.4.4 Creating a Mentor

The Mentor class contains a method that takes the builder instance object as a parameter, sets up the customer phone call, text message, and ring tone information, and then returns to the mobile plan instance.

 PackageCom.demo.builder.director;ImportCom.demo.builder.itf.IMobileBuilder;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/14. * Mobile Package Guide*/ Public classMobiledirector { Publicmobilepackage createmobilepackage (Imobilebuilder mobilebuilder) {if(Mobilebuilder! =NULL){            //Build your phoneMobilebuilder.buildmoney (); //Build SMSMobilebuilder.buildshortinfo (); //Building RingtonesMobilebuilder.buildmusic (); //back to Mobile package            returnMobilebuilder.getmobilepackage (); }        return NULL; }}

4.4.5 Customer Order Mobile Package

ImportCom.demo.builder.director.MobileDirector;Importcom.demo.builder.itf.MobileBuilderImpl1;Importcom.demo.builder.itf.MobileBuilderImpl2;ImportCom.demo.builder.model.MobilePackage;/*** Created by LSQ on 2018/3/13. **/ Public classMainapp { Public Static voidMain (string[] args) {//Create a MentorMobiledirector Mobiledirector =NewMobiledirector (); //Set 1MOBILEBUILDERIMPL1 MOBILEBUILDERIMPL1 =NewMobileBuilderImpl1 (); //Set 2MOBILEBUILDERIMPL2 MOBILEBUILDERIMPL2 =NewMobileBuilderImpl2 ();        Printmessage (Mobiledirector.createmobilepackage (MOBILEBUILDERIMPL1));    Printmessage (Mobiledirector.createmobilepackage (MOBILEBUILDERIMPL2)); }    /*** Print out package information*/     Public Static voidprintmessage (Mobilepackage mobilepackage) {System.out.println (--Phone: "+mobilepackage.getmoney () +" \ t SMS: "+mobilepackage.getshortinfo () +" T-ring: "+Mobilepackage.getmusic ()); }}

Execution Result:

Implementing the Code structure:

From the above code is not difficult to see, in fact, the guide is only an intermediary service, and ultimately the builder set the instance object, and finally back to the end customer, then why do you want to mentor role?

The instructor is indeed an intermediary service, but its role is not to be underestimated, it is the role of the mentor to build a product process for the builder package, effectively reducing the client to use the builder to build the product of the various processes. If the construction of a complex product requires more than 10 processes or more, if the client steps through the use of builders to build complex objects, then the complexity of the client can be imagined, the most important thing is that the client needs the final product, build process client does not need to care. Therefore, there is a certain reason for the existence of the role of the mentor.

4.6 Use Cases

1) When the resulting product object has a complex structure inside;

2) When complex objects need to be separated from the presentation, they may need to create different representations;

3) When you need to hide the performance of the internal structure of the product to the client.

The advantages of the builder model lie in the separation of the construction and representation, effectively decomposing the complex object processing process, reducing the coupling degree between the functional modules, and enhancing the internal cohesion of the module, which makes it extremely important in the software design mode. The builder design pattern is shown in structure 4-6:

  

In fact, the builder model simplifies processing and removes the role of the mentor. 4-7 is shown below:

  

After that, you can simply understand the builder pattern: There is an object instance of a specific product within the builder, and all of the build functions are for this object instance, and this object instance is what we ultimately want or other representations of that object instance. This is equivalent to first in the tank to fill enough water, until you want to drink water when you do not have to open the tap to drink, directly from the tank to scoop up water on the line. Of course, even if there is not enough water in the tank (the individual functions of the building object are independent of each other), you can still drink water (construction and presentation separation).

Extension: The difference between builder mode and abstract factory pattern

1) The builder model focuses on the construction of a complex object in steps, while the abstract factory model focuses on the construction of multiple series of product objects i.e. object families;

2) The builder mode is the final step back to the specific product, while the abstract Factory mode returns the product immediately.

04-Mobile Package: Builder mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.