建造者模式(Builder)

建造者模式(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

    原文作者:Knowing
    原文地址: https://segmentfault.com/a/1190000014892929
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞