Preliminary Exploration of Design Pattern-Builder Pattern

Source: Internet
Author: User

Builder mode, also known as Builder or Builder mode, is an object creation mode that focuses on building complex objects step by step. The generated objects are returned only after the build is complete. The Builder mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

I. Application scenarios

1. When an algorithm for creating a complex object is independent of the components of the object and their assembly methods, for example, when I buy a computer, I don't need to know the computer CPU, motherboard, how the chassis is produced, and you do not need to know how they are assembled.

2. When the construction process must allow different representations of the constructed object, for example, a computer can use amd cpu or Inter CPU, these can be changed according to requirements.

Ii. UML diagram


Iii. Java implementation

Example 1:

Package study. patterns. builder;/*** builder mode: separates the construction of a complex object from its representation. * This allows you to create different representations for the same build process. * @ Author qbg **/public class BuilderPattern {/*** chassis abstract class */abstract class Crate {@ Overridepublic String toString () {return "abstract crate... ";}}/**** cool and cold extreme chassis */class CoolerMasterCrate extends Crate {@ Overridepublic String toString () {return" CoolerMaster crate... ";}}/*** CPU abstract class */abstract class CPU {@ Overridepublic String toString () {return" abstract cpu... ";}}/**** amd cpu */class AMDCPU extends CPU {@ Overridepublic String toString () {return "AMD cpu... ";}}/*** main board abstract class */abstract class Motherboard {@ Overridepublic String toString () {return" abstract motherboard... ";}}/**** ASUS Motherboard */class ASUSMotherboard extends motherboard {@ Overridepublic String toString () {return" ASUS Motherboard... ";}}/*** Computer definition class */class Computer {CPU cpu; // cpuMotherboard motherboard; // motherboard Crate crate; // chassis @ Overridepublic Str Ing toString () {return "CPU:" + cpu + "\ nMotherboard:" + motherboard + "\ nCrate:" + crate ;}/ *** interface, CPU and other components can be produced in factory mode to further separate the process of product component generation and construction... */interface ComputerBuilder {public ComputerBuilder buildCPU (); public ComputerBuilder buildMotherboard (); public ComputerBuilder buildCrate (); public Computer getProduct ();} /*** most popular desktop build implementation class */class hot‑computerbuilder implements ComputerBuilder {private Computer Computer; public hotbench computerbuilder () {this. computer = new Computer () ;}@ Overridepublic ComputerBuilder buildCPU () {computer. cpu = new AMDCPU (); return this ;}@ Overridepublic ComputerBuilder buildMotherboard () {computer. motherboard = new ASUSMotherboard (); return this ;}@ Overridepublic ComputerBuilder buildCrate () {computer. crate = new CoolerMasterCrate (); return this ;}@ Overridepublic Computer getProd Uct () {buildMotherboard (). buildCPU (). buildCrate (); return computer ;}/ *** pilot, the client obtains the product through the pilot, * regardless of how the pilot gets the product object built by the Builder. The specific construction process is handed over to the specific Builder. */Class Director {private ComputerBuilder builder; public Director (ComputerBuilder builder) {this. builder = builder;}/*** return the constructed product */public Computer construct () {return builder. getProduct () ;}} public static void main (String [] args) {BuilderPattern bp = new BuilderPattern (); ComputerBuilder builder = bp. new hotplug computerbuilder (); Director ctor = bp. new Director (builder); Computer computer = director. construct (); System. out. println (computer );}}
When a complex object has multiple variable parameters and may be added later, it is best to use the Builder mode to build the object, as shown below:

Example 2:

Package study. patterns. builder;/*** Builder mode, used to build a large number of optional parameter objects (more than four or internal attributes are often extended) * @ author qbg * unchangeable class */public class NutritionFacts {private final int servings; // private final int calories is required for each part; // private final int fat is optional for calories; // optional private final int sodium for fat; // optional private final int carbohydrate for sodium; // optional public static class Builder for carbohydrate {// required parameter private final int servings; // optional parameter private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; public Builder (int servings) {this. servings = servings;} public Builder calories (int cal) {calories = cal; return this;} public Builder fat (int fat) {this. fat = fat; return this;} public Builder sodium (int sod) {this. sodium = sod; return this;} public Builder carbohydrate (int car) {this. carbohydrate = car; return this;} public NutritionFacts build () {return new NutritionFacts (this) ;}/ ** private constructor, this object can only be created through the Builder */private NutritionFacts (builder) {servings = Builder. servings; calories = builder. calories; fat = builder. fat; sodium = builder. sodium; carbohydrate = builder. carbohydrate;} public void print () {System. out. println ("servings:" + servings); System. out. println ("calories:" + calories); System. out. println ("fat:" + fat); System. out. println ("sodium:" + sodium); System. out. println ("carbohydrate:" + carbohydrate);} public static void main (String [] args) {NutritionFacts cocaCola = new NutritionFacts. builder (0, 600 ). calories (20 ). carbohydrate (300 ). build (); cocaCola. print ();}}
Iv. Advantages and Disadvantages

Advantages:

1. The internal representation of a product can be changed. For example, if a new ComputerBuilder is implemented in the example, the internal components of the computer can be changed, because the Computer Representation and internal structure are independent of the client code.

2. Separate the constructor code from the representation code. The constructor code is equivalent to builder and the code is equivalent to ctor. Different directors can use the same generator to construct different products.

3. We can control the construction process in more detail. For example, in Example 2, we can add validation to the construction process.

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.