This article describes the use of builder mode to implement the function of applying for mobile phone packages. Share it for your reference, as follows:
1. Pattern definition
The builder simply understands it to build things, but the builder model is not a simple thing, but a relatively complex thing.
2. Examples of the model
1 Pattern Analysis
We borrow an example of a customer going to the mobile phone package to apply for a mobile phone package to illustrate this model after abstract analysis, we obtain the following information about customer Zhang San - the terminal demand business hall operator - the instructor (notifying the builder what kind of mobile phone package to produce)
Computer - Builder (build various mobile phone packages)
Mobile phone package – product
2 Builder Mode Static Modeling
3 Code Examples
3.1 Create a product - Mobile phone package
package com.demo.buider.model;public class MobilePackage{ // phone bill private float money; // SMS private int shortInfo; // Cailing private String music; public float getMoney() { return money; } public void setMoney(float money) { this.money = money; } public int getShortInfo() { return shortInfo; } public void setShortInfo(int shortInfo) { this.shortInfo = shortInfo; } public String getMusic() { return music; } public void setMusic(String music) { this.music = music; }}3.2 Builder Interface
package com.demo.buider.itf;import com.demo.buider.model.MobilePackage;/** * Mobile Package Builder * * @author * */public interface IMobileBuilder{ // Phone fee for building mobile package public void buildMoney(); // SMS for building mobile package public void buildShortInfo(); // Cailing public void buildMusic(); // Return to the built mobile package object public MobilePackage getMobilePackage();}3.3 Establish a specific builder
Specific Builder 1
package com.demo.buider.itf;import com.demo.buider.base.AbstractBasePackage;import com.demo.buider.model.MobilePackage;/** * Package 1 * * @author maofw * */public class MobileBuilderImpl1 extends AbstractBasePackage implements IMobileBuilder{ // Phone fee for building mobile phone package public void buildMoney() { this.mobilePackage.setMoney(20.0f); } // Cailing public void buildMusic() { this.mobilePackage.setMusic("angel"); } // SMS for building mobile package public void buildShortInfo() { this.mobilePackage.setShortInfo(400); } // Return to the built mobile package object public MobilePackage getMobilePackage() { return this.mobilePackage; }}Specific Builder 2
package com.demo.buider.itf;import com.demo.buider.base.AbstractBasePackage;import com.demo.buider.model.MobilePackage;/** * Package 2 * * @author maofw * */public class MobileBuilderImpl2 extends AbstractBasePackage implements IMobileBuilder{ // Phone fee for building mobile phone package public void buildMoney() { this.mobilePackage.setMoney(30.0f); } // Cailing public void buildMusic() { this.mobilePackage.setMusic("Sea"); } // SMS for building mobile package public void buildShortInfo() { this.mobilePackage.setShortInfo(600); } // Return to the built mobile package object public MobilePackage getMobilePackage() { return this.mobilePackage; }}3.4 Creating a mentor
package com.demo.buider.director;import com.demo.buider.itf.IMobileBuilder;import com.demo.buider.model.MobilePackage;/** * Mobile package instructor* * @author maofw * */public class MobileDirector{ public MobilePackage createMobilePackage(IMobileBuilder mobileBuilder) { if (mobileBuilder != null) { // build phone bill mobileBuilder.buildMoney(); // build text message mobileBuilder.buildShortInfo(); // Build Cailing mobileBuilder.buildMusic(); // Return to mobile package return mobileBuilder.getMobilePackage(); } return null; }}Let customers order mobile phone packages
package com.demo.buider;import com.demo.buider.director.MobileDirector;import com.demo.buider.itf.MobileBuilderImpl1;import com.demo.buider.itf.MobileBuilderImpl2;import com.demo.buider.model.MobilePackage;public class MainApp{ /** * main application* * @param args */ public static void main(String[] args) { // Create a mentor MobileDirector mobileDirector = new MobileDirector(); // Plan 1 MobileBuilderImpl1 mobileBuilderImpl1 = new MobileBuilderImpl1(); // Plan 2 MobileBuilderImpl2 mobileBuilderImpl2 = new MobileBuilderImpl2(); printMessage(mobileDirector.createMobilePackage(mobileBuilderImpl1)); printMessage(mobileDirector.createMobilePackage(mobileBuilderImpl2)); } /** * Printout package information*/ public static void printMessage(MobilePackage mobilePackage) { System.out.println("--Telcoding:" + mobilePackage.getMoney() + "/t SMS:" + mobilePackage.getShortInfo() + "Twitter/t Cailing:" + mobilePackage.getMusic()); }}Running results
--Tel: 20.0 SMS: 400 Cailing: Angel
--Tel: 30.0 SMS: 600 Cailing: Dahai
3. The principle of setting the model
1 step to create complex objects
2. Construct and represent separation
3 Single responsibility principle
4. Use occasions
1 When the product object has a complex structure inside it
2 When complex objects need to be separated from the representation, they may need to create different representations
3 When it is necessary to hide product structural performance from customers
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.