Design Mode (5): builder mode, design mode construction mode

Source: Internet
Author: User

Design Mode (5): builder mode, design mode construction mode

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.

 

Class diagram:

 

 

 

Four roles:

1. Product Products
The template method mode is usually implemented.

2. Builder abstract Builder
The establishment of standardized products is generally implemented by sub-classes.

3. ConcreateBuilder
Implements all methods defined by abstract classes and returns an object of good components.

4. Director class
Arranges the order of existing modules and tells Builder to start construction.

 

// Product class Product {public void doSomething () {// Independent Service Processing} // abstract Builder abstract class Builder {// sets different parts of the Product, to obtain different products/different parts, it may be different parts or different public abstract void setPart () assembly sequence (); // construct the Product public abstract Product buildProduct ();} // The specific Builder class ConcreateProduct extends Builder {private product Product = new Product (); // set the Product part public void setPart () {// logical processing in the Product class} // component a product's public Product buildProduct () {return product ;}/// director class // director class serves as an encapsulation, avoid high-level modules going deep into the Builder's internal implementation class Director {private builder = new ConcreateProduct (); // construct different products public Product getAProduct () {Builder. setPart (); return builder. buildProduct ();}}


 

The main function of the builder mode is to arrange the call sequence of basic methods. That is to say, these basic methods have been implemented. Generally speaking, parts are assembled in different order and different objects are generated. The factory method focuses on creation, creating a part is its primary responsibility, and the assembly sequence is not of its concern.

 

A complete class diagram:

 

 

Code:

 

Abstract class CarBuilder {// construct a model that is to assemble the sequence public abstract void setSequence (ArrayList <String> sequence); public abstract CarModel getCarModel ();} class BenzBuilder extends CarBuilder {private BenzModel benz = new BenzModel (); @ Overridepublic void setSequence (ArrayList <String> sequence) {this. benz. setSequence (sequence) ;}@ Overridepublic CarModel getCarModel () {return this. benz ;}} class BMWBuilder extends CarBuilder {private BMWModel bmw = new BMWModel (); @ Overridepublic void setSequence (ArrayList <String> sequence) {this. bmw. setSequence (sequence) ;}@ Overridepublic CarModel getCarModel () {return this. bmw ;}} abstract class CarModel {private ArrayList <String> sequence = new ArrayList (); protected abstract void start (); protected abstract void stop (); protected abstract void alarm (); protected abstract void engineBoom (); final public void run () {for (int I = 0; I <sequence. size (); I ++) {String actionName = this. sequence. get (I); if (actionName. equalsIgnoreCase ("start") {this. start ();} else if (actionName. equalsIgnoreCase ("stop") {this. stop ();} else if (actionName. equalsIgnoreCase ("alarm") {this. alarm ();} else if (actionName. equalsIgnoreCase ("engine Boom") {this. engineBoom () ;}} final public void setSequence (ArrayList <String> sequence) {this. sequence = sequence;} class BenzModel extends CarModel {@ Overrideprotected void start () {System. out. println ("Benz ----> Start") ;}@ Overrideprotected void stop () {System. out. println ("Benz ----> stop") ;}@ Overrideprotected void alarm () {System. out. println ("Benz ----> horn") ;}@ Overrideprotected void engineBoom () {System. out. println ("Benz ----> engine sound");} class BMWModel extends CarModel {@ Overrideprotected void start () {System. out. println ("BMW ----> Start") ;}@ Overrideprotected void stop () {System. out. println ("BMW ----> stop") ;}@ Overrideprotected void alarm () {System. out. println ("BMW ----> horn") ;}@ Overrideprotected void engineBoom () {System. out. println ("BMW ----> engine sound") ;}} class ctor {private ArrayList <String> sequence = new ArrayList <String> (); private BenzBuilder benzBuilder = new BenzBuilder (); private BMWBuilder bmwBuilder = new BMWBuilder (); public BenzModel getABenzModel () {this. sequence. clear (); this. sequence. add ("start"); this. sequence. add ("stop"); this. benzBuilder. setSequence (sequence); return (BenzModel) this. benzBuilder. getCarModel ();} public BenzModel getBBenzModel () {this. sequence. clear (); this. sequence. add ("engine boom"); this. sequence. add ("start"); this. benzBuilder. setSequence (sequence); return (BenzModel) this. benzBuilder. getCarModel ();} public BMWModel getCBmwModel () {this. sequence. clear (); this. sequence. add ("alarm"); this. sequence. add ("start"); this. sequence. add ("stop"); this. bmwBuilder. setSequence (sequence); return (BMWModel) this. bmwBuilder. getCarModel ();} public BMWModel getDBmwModel () {this. sequence. clear (); this. sequence. add ("start"); this. bmwBuilder. setSequence (sequence); return (BMWModel) this. bmwBuilder. getCarModel () ;}} public class Client {public static void main (String [] args) {Director ctor = new director (); System. out. println ("-------------------------------------"); for (int I = 0; I <10; I ++) {System. out. print (I + 1) + "+"); director. getABenzModel (). run ();} System. out. println ("-------------------------------------"); for (int I = 0; I <5; I ++) {System. out. print (I + 1) + "+"); director. getBBenzModel (). run ();} System. out. println ("-------------------------------------"); for (int I = 0; I <10; I ++) {System. out. print (I + 1) + "+"); director. getCBmwModel (). run ();} System. out. println ("-------------------------------------"); for (int I = 0; I <5; I ++) {System. out. print (I + 1) + "+"); director. getDBmwModel (). run ();}}}


 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.