建造者模式(Builder)

Seasons20發表於2019-01-19

建造者模式(Builder)

一.一般實現

1.1 建立簡單物件

    public class SimpleEntity1 {
        public SimpleEntity1(){
            System.out.println("create SimpleEntity1...");
        }
    }
    
    public class SimpleEntity2 {
        public SimpleEntity2(){
            System.out.println("create SimpleEntity2...");
        }
    }

1.2 建立複雜物件

    public class ComplexEntity {
        public void createComplexEntity(SimpleEntity1 simpleEntity1, SimpleEntity2 simpleEntity2){
            System.out.println("createComplexEntity ... with simpleEntity1 AND simpleEntity2 ");
        }
    }

1.3 呼叫

    public static void main(String[] args) {
        SimpleEntity1 s1 = new SimpleEntity1();
        SimpleEntity2 s2 = new SimpleEntity2();
        ComplexEntity complexEntity = new ComplexEntity();
        complexEntity.createComplexEntity(s1, s2);
    }

1.4 輸出

    create SimpleEntityA1...
    create SimpleEntity2...
    createComplexEntity ... with simpleEntity1 AND simpleEntity2 

1.5 缺點

  1. 物件耦合度過高,簡單物件,複雜物件都與呼叫程式耦合.
  2. 當簡單物件組合的方式發生變化時,由其組合建立的複雜物件也跟著發生變化.

二.建造者模式

2.1 定義

將一個複雜的物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示.

2.2 適用情況

  1. 需要生成的物件具有複雜的內部結構.
  2. 需要生成的物件內部屬性本身互相依賴.

2.3 角色

  1. Builder:建立產品物件的公共介面.
  2. ConcreteBuilder:實現Builder介面併產生具體的簡單物件,以供合成複雜物件.
  3. Director:呼叫具體建造者,指導產生過程.
  4. Product:最終產生的複雜產品物件.

三.程式碼實現

3.1 複雜產品物件

    public class ComplexEntity {
        public ComplexEntity(){
            System.out.println("create complexEntity ...");
        }
        public void setSimpleEntityA(ISimpleEntityA a){
            System.out.println("complexEntity set simpleEntityA ...");
        }
        public void setSimpleEntityB(ISimpleEntityB b){
            System.out.println("complexEntity set simpleEntityB ...");
        }
    }

3.2 簡單產品

    public interface ISimpleEntityA {
    }
    
    public interface ISimpleEntityB {
    }
    
    public class SimpleEntityA1 implements ISimpleEntityA {
        public SimpleEntityA1(){
            System.out.println("create SimpleEntityA1 ...");
        }
    }
    
    public class SimpleEntityB1 implements ISimpleEntityB {
        public SimpleEntityB1(){
            System.out.println("create SimpleEntityB1 ...");
        }
    }

3.3 抽象Builder介面

    public interface IBuilder {
        void createSimpleEntityA();
        void createSimpleEntityB();
        ComplexEntity createComplexEntity();
    }

3.4 具體建造者物件

    public class Builder1 implements IBuilder{
        ComplexEntity complexEntity = new ComplexEntity();
        @Override
        public void createSimpleEntityA() {
            complexEntity.setSimpleEntityA(new SimpleEntityA1());
        }
        @Override
        public void createSimpleEntityB() {
            complexEntity.setSimpleEntityB(new SimpleEntityB1());
        }
        @Override
        public ComplexEntity createComplexEntity() {
            return complexEntity;
        }
    }

3.5 指導者物件

    public class Director {
        public ComplexEntity create1(){
            IBuilder builder = new Builder1();
            builder.createSimpleEntityA();
            builder.createSimpleEntityB();
            return builder.createComplexEntity();
        }
    }

3.6 呼叫

    public static void main(String[] args) {
        Director director = new Director();
        director.create1();
    }

3.7 輸出

    create complexEntity ...
    create SimpleEntityA1 ...
    complexEntity set simpleEntityA ...
    create SimpleEntityB1 ...
    complexEntity set simpleEntityB ...

3.8 優點

  1. 易於擴充套件(新增具體建造者).
  2. 易於解耦(產品本身與產品建立過程).
  3. 精確控制複雜物件的建立.

3.9 缺點

  1. 範圍(簡單物件需有共同點).
  2. 內部變化不能過於複雜(會產生大量具體建造者).

四.原始碼

https://github.com/Seasons20/DisignPattern.git

END

相關文章