23 Design Patterns (5): Builder Pattern

1: Definition
        of Builder Pattern ---> Builder Pattern, also known as Builder Pattern, is defined as follows: the construction of a complex object is separated from its representation, so that the same construction process can create different representations
        ● Product The product class
                usually implements the template method pattern, that is, there are template methods and basic methods, which refer to the template method pattern. The BenzModel and BMWModel in the example belong to the product category.
        ● Builder Abstract Builder
                standardizes the formation of products, which is generally realized by subclasses. CarBuilder in the example is an abstract builder.
        ● ConcreteBuilder The concrete builder
                implements all the methods defined by the abstract class and returns an assembled object. BenzBuilder and BMWBuilder in the example belong to concrete builders.
        ● The Director class
                is responsible for arranging the order of the existing modules, and then telling the Builder to start building

II: The advantages of the builder pattern

        ● The encapsulation of
                the builder pattern can make the client do not need to know the details of the internal composition of the product, such as in the example we do not need Concerning how each specific model is implemented internally, the resulting object type is CarModel.
        ● The builder is independent and easy to expand
                . BenzBuilder and BMWBuilder are independent of each other, which is very beneficial to the expansion of the system.
        ●Easy to control the risk of detail
                Since the specific builder is independent, the construction process can be gradually refined without any impact on other modules.

Three: Builder mode usage scenarios

        ● The builder mode can be used when the same method, different execution order, and different event results are generated.
        ● This mode can be used when multiple parts or parts can be assembled into one object, but the operating results are not the same.
        ● The product class is very complex, or the calling sequence in the product class produces different performances. At this time, it is very suitable to use the builder pattern.
        ● Some other objects in the system will be used in the process of object creation. When these objects are not easy to obtain in the process of creation of product objects, the creation process of the object can also be encapsulated by the builder mode. This kind of scenario can only be a compensation method, because an object is not easy to obtain, and it is not discovered in the design stage, and the creation process should be softened by the creator mode, which itself violates the original goal of the design.


Four: Notes

        on the builder pattern ---> The builder pattern focuses on the part type and assembly process (sequence), which is the biggest difference between it and the factory method pattern. Although it is the same as the creation class pattern, the focus is different .
        ---> This builder pattern is very similar to the factory pattern, yes, very similar, but remember one thing and you can use it with ease: the main function of the builder pattern is the calling sequence of the basic methods, that is These basic methods have been implemented, in layman's terms, it is the assembly of parts, and the objects generated in different sequences are also different; while the factory method focuses on creation, creating parts is its main responsibility, and the assembly sequence is not what it cares about.
        ---> Again, consider the template method pattern when using the builder pattern, don't think about a pattern in isolation, rigidly applying a pattern will make you suffer endlessly!

Five: Examples of the builder pattern



Template algorithm abstract class for template method pattern

1 package com.yeepay.sxf.template6;
 2 
 3 import java.util.ArrayList;
 4 
 5 /**
 6  * 算法模板类
 7  * @author sxf
 8  *
 9  */
10 public abstract class CarModel {
11     
12     //具体核心算法步骤执行顺序的调度
13     private ArrayList<String> sequence;
14     //汽车启动算法
15     protected abstract void start();
16     //汽车停止算法
17     protected abstract void stop();
18     //汽车喇叭鸣叫算法
19     protected abstract void alarm();
20     //引擎哄哄响算法
21     protected abstract void engineBoom();
22     
23     //算法组装实现方法
24     final public void run(){
25         //循环一边,谁在前执行谁
26         for(int i=0;i<this.sequence.size();i++){
27             String actionName=this.sequence.get(i);
28             if(actionName.equalsIgnoreCase("start")){
29                 this.start();
30             }else if(actionName.equalsIgnoreCase("stop")){
31                 this.stop();
32             }else if(actionName.equalsIgnoreCase("alarm")){
33                 this.alarm();
34             }else if(actionName.equalsIgnoreCase("engineBoom")){
35                 this.engineBoom();
36             }
37         }
38     }
39     /**
40      * 给予执行算法
41      * @param sequence
42      */
43     final public void setSequence(ArrayList<String> sequence){
44         this.sequence=sequence;
45     }
46 }
奔驰算法类
1 package com.yeepay.sxf.template6;
 2 /**
 3  * 奔驰车的具体算法实现
 4  * @author sxf
 5  *
 6  */
 7 public class BenzModel extends CarModel {
 8 
 9     @Override
10     protected void start() {
11         System.out.println("BenzModel.start(==》奔驰车算法启动)");
12     }
13 
14     @Override
15     protected void stop() {
16         System.out.println("BenzModel.stop(==》奔驰车算法停止)");
17     }
18 
19     @Override
20     protected void alarm() {
21         System.out.println("BenzModel.alarm(==》奔驰算法喇叭鸣叫)");
22     }
23 
24     @Override
25     protected void engineBoom() {
26         System.out.println("BenzModel.engineBoom(==》奔驰算法发动机轰鸣)");
27     }
28 
29     
30 }
宝马算法类
 
 1 package com.yeepay.sxf.template6;
 2 /**
 3  * 宝马车算法实现
 4  * @author sxf
 5  *
 6  */
 7 public class BMWModel extends CarModel{
 8 
 9     @Override
10     protected void start() {
11         System.out.println("BMWModel.start(==》宝马车算法启动)");
12     }
13 
14     @Override
15     protected void stop() {
16         System.out.println("BMWModel.stop(==》宝马车算法停止)");
17     }
18 
19     @Override
20     protected void alarm() {
21         System.out.println("BMWModel.alarm(==》宝马车算法喇叭鸣叫)");
22     }
23 
24     @Override
25     protected void engineBoom() {
26         System.out.println("BMWModel.engineBoom(==>宝马车算法引擎轰鸣)");
27     }
28     
29 }
 
建造者模式的建造抽象类
1 package com.yeepay.sxf.template6;
 2 
 3 import java.util.ArrayList;
 4 
 5 /**
 6  * 汽车算法构建抽象
 7  * @author sxf
 8  *
 9  */
10 public abstract class CarBuilder {
11     /**
12      * 建造一个模型,你要给我一个顺序要求,就是组装的算法
13      * @param sequence
14      */
15     public abstract void setSequence(ArrayList<String> sequence);
16     /**
17      * 获取组装好的算法体
18      * @return
19      */
20     public abstract CarModel getCardModel();
21 }
22     
copy code

奔驰算法建造类实现

 
 1 package com.yeepay.sxf.template6;
 2 
 3 import java.util.ArrayList;
 4 
 5 /**
 6  * 奔驰车算法的构建实现
 7  * @author sxf
 8  *
 9  */
10 public class BenzBuilder extends CarBuilder{
11     //奔驰的算法体
12     private BenzModel benz=new BenzModel();
13     
14     //给奔驰具体的算法执行
15     @Override
16     public void setSequence(ArrayList<String> sequence) {
17         this.benz.setSequence(sequence);
18     }
19 
20     //获取奔驰的算法体
21     @Override
22     public CarModel getCardModel() {
23         return this.benz;
24     }
25 
26 
27 }
宝马算法建造类实现    
 
 1 package com.yeepay.sxf.template6;
 2 
 3 import java.util.ArrayList;
 4 /**
 5  * 宝马算法构建类
 6  * @author sxf
 7  *
 8  */
 9 public class BMWBuilder extends CarBuilder{
10     //宝马的算法体
11     private BMWModel bmwModel=new BMWModel();
12 
13     //给宝马算法体具体的算法执行
14     @Override
15     public void setSequence(ArrayList<String> sequence) {
16         this.bmwModel.setSequence(sequence);
17     }
18     //获取宝马的算法体
19     @Override
20     public CarModel getCardModel() {
21         return this.bmwModel;
22     }
23     
24     
25 }
 
导演类  
 
 1 package com.yeepay.sxf.template6;
 2 
 3 import java.util.ArrayList;
 4 
 5 /**
 6  * 导演类
 7  * @author sxf
 8  *
 9  */
10 public class Director {
11     private ArrayList<String> sequence=new ArrayList<String>();
12     private BenzBuilder benzBuilder=new BenzBuilder();
13     private BMWBuilder bmwBuilder=new BMWBuilder();
14     
15     /**
16      * 获取A类型奔驰的算法体
17      * @return
18      */
19     public BenzModel getABenzModel(){
20         //清理算法执行顺序
21         this.sequence.clear();
22         this.sequence.add("start");
23         this.sequence.add("stop");
24         this.benzBuilder.setSequence(sequence);
25         return (BenzModel) this.benzBuilder.getCardModel();
26         
27     }
28     
29     /**
30      * 获取B类奔驰的算法体
31      * @return
32      */
33     public BenzModel getBBenzModel(){
34         //清理算法执行顺序
35         this.sequence.clear();
36         this.sequence.add("start");
37         this.sequence.add("stop");
38         this.sequence.add("alarm");
39         this.benzBuilder.setSequence(sequence);
40         return (BenzModel) this.benzBuilder.getCardModel();
41     }
42     /**
43      * 获取A类型宝马的算法体
44      * @return
45      */
46     public BMWModel getABMWModel(){
47         this.sequence.clear();
48         this.sequence.add("start");
49         this.sequence.add("alarm");
50         this.sequence.add("stop");
51         this.bmwBuilder.setSequence(sequence);
52         return (BMWModel) this.bmwBuilder.getCardModel();
53     }
54 }
 
客户端测试类
1 package com.yeepay.sxf.template6;
 2 /**
 3  * 客户端
 4  * @author sxf
 5  *
 6  */
 7 public class Client {
 8 
 9     public static void main(String[] args) {
10         //获取导演类
11         Director director=new Director();
12         
13         //由导演类获取A类型奔驰算法,并执行
14         CarModel aBenz=director.getABenzModel();
15         aBenz.run();
16         
17         //由导演类获取A类型宝马算法,并执行
18         CarModel aBMW=director.getABMWModel();
19         aBMW.run();
20     }
21 }
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488486&siteId=291194637