Freely scale your project: Builder mode, extended builder

Source: Internet
Author: User
Tags macbook

Freely scale your project: Builder mode, extended builder

Because a complex object has many components, such as a car, a wheel, a steering wheel, an engine, and various small parts, how can we assemble these components into a car, this assembly process is long and complex. In this case, in order to hide implementation details from the outside during the construction process, you can use the Builder mode to separate parts from the assembly process, this allows both the building process and components to be freely scalable, and minimizes the coupling between the two.

1. Builder mode definition

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

2. When the Builder mode uses the same method and different execution sequence to generate different event results. Multiple parts or parts can be assembled into an object, but the running results are not the same. The product category is very complex, or the calling sequence in the product category is different, which makes it very suitable to use the builder mode. When initializing an object is complex, such as multiple parameters, and many parameters have default values. 3. Builder mode UML diagram

4. Builder mode implementation
// Computer abstract class, that is, the Product role public abstract class Computer {protected String mBoard; protected String mDisplay; protected String mOS; protected Computer () {}// set the motherboard public void setBoard (String board) {mBoard = board;} // set the display public void setDisplay (String display) {mDisplay = display ;} // set the OS public void setOS () {mOS = OS;} @ Override public String toString () {return "Computer [mBoard =" + mBoard + ", mDisplay = "+ mDisplay +", mOS = "+ mOS +"] ";}}
// Specific Computer class, Macbookpublic class Macbook extends Computer {protected Macbook () {}@ Override public void setOS () {mOS = "Mac OS X 10.10 ";}}
// Abstract Builder class public abstract class Builder {// set the host public abstract void buildBoard (String board); // set the display public abstract void buildDisplay (String diaplay ); // set the operating system public abstract void buildOS (); // create Comuter public abstract Computer create ();}
// Specific Builder class, MacbookBuilderpublic class MacbookBuilder extends Builder {private Computer mComputer = new Macbook (); @ Override public void buildBoard (String board) {mComputer. setBoard (board) ;}@ Override public void buildDisplay (String diaplay) {mComputer. setDisplay (diaplay) ;}@ Override public void buildOS () {mComputer. setOS () ;}@ Override public Computer create () {return mComputer ;}}
// Director class, responsible for building Computerpublic class Director {Builder mBuilder = null;/*** @ param builder */public Director (Builder builder) {mBuilder = builder ;} /*** build object */public void construct (String board, String display) {mBuilder. buildBoard; mBuilder. buildDisplay (display); mBuilder. buildOS ();}}
// Test class public class Test {public static void main (String [] args) {// constructor Builder = new MacbookBuilder (); Director pcDirector = new Director (builder ); // encapsulate the build process, 4-core, 2 GB memory, and Mac system pcDirector. construct ("Intel motherboard", "Retina display"); // creates a computer and outputs information about the System. out. println ("Computer Info:" + builder. create (). toString ());}}
5. Summary

The Builder mode is also commonly used in Android development. It is usually used as the Builder of the configuration class to extract the construction and representation of the configuration, and also to isolate the configuration from the target class, avoid too many setter methods. The Builder mode is commonly implemented through the call chain, which makes the code simpler and easier to understand.

Advantages
1. Good encapsulation. Using the builder mode, the client does not have to know the details of the product composition.
2. the builder is independent and easy to expand.

Disadvantages
Excessive Builder objects and ctor objects are generated, consuming memory.

 

Related Article

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.