设计模式剖析系列之建造者模式

By heiry on 2019-08-05 [ in 技术 ]

建造者模式

建造者(Builder)模式的主要模块如下:

1. 建造者接口:负责建造各个组件

2.装配者接口:负责将组件组装成产品

3.产品类:成员变量为各组件对象

4.各个组件类

5. 建造者实现类:实现建造者接口

6.装配者实现类:实现装配者接口

7.客户端调用类

这里以建造超级计算机为例,演示构建者模式原理:

假设计算机由CPU/主板/内存三个部件组成,现在要生产一台陌桑超级计算机,CPU为AMD的CPU,主板为华硕主板,内存为256G容量内存。类关系图如下:

使用建造者模式具体做法:

package net.mosang.builder;
/**
 * 具体产品类
 * @author heiry
 *
 */

public class Computer {
  private CPU cpu;
  private Mainboard mainboard;
  private Memory memory;
  
  public CPU getCpu() {
    return cpu;
  }
  public void setCpu(CPU cpu) {
    this.cpu = cpu;
  }
  public Mainboard getMainboard() {
    return mainboard;
  }
  public void setMainboard(Mainboard mainboard) {
    this.mainboard = mainboard;
  }
  public Memory getMemory() {
    return memory;
  }
  public void setMemory(Memory memory) {
    this.memory = memory;
  }
  
}
package net.mosang.builder;
/**
 * 构建子组件接口
 * @author heiry
 *
 */
public interface ComputerBuilder {
  public CPU buildCPU();
  public Mainboard buildMainboard();
  public Memory buildMemory();
}
package net.mosang.builder;
/**
 * 装配者接口
 * @author heiry
 *
 */
public interface ComputerDirector {
  public abstract Computer directComputer();

}
package net.mosang.builder;
/**
 * 构建者实现类
 * 构建陌桑超级计算机部件
 * @author wuah
 *
 */
public class MosangSuperComputer implements ComputerBuilder {

  @Override
  public CPU buildCPU() {
    System.out.println("陌桑超算使用的是AMD的CPU");
    return new CPU("AMD");
  }

  @Override
  public Mainboard buildMainboard() {
    System.out.println("陌桑超算使用的是华硕主板");
    return new Mainboard("华硕");
  }

  @Override
  public Memory buildMemory() {
    System.out.println("陌桑超算使用的是256G内存");
    return new Memory(256);
  }
}
package net.mosang.builder;
/**
 * 装配者实现类:返回具体产品实例
 * 陌桑超算由富士康代工装配
 * @author heiry
 *
 */
public class FoxconnDirector implements ComputerDirector {
  private  ComputerBuilder builder;
  @Override
  public Computer directComputer() {
    CPU cpu = builder.buildCPU();
    Mainboard mainboard = builder.buildMainboard();
    Memory memory = builder.buildMemory();
    Computer c = new Computer();
    c.setCpu(cpu);
    c.setMainboard(mainboard);
    c.setMemory(memory);
    return c;
  }
  
  public FoxconnDirector(ComputerBuilder builder) {
    super();
    this.builder = builder;
  }
}
package net.mosang.builder;
/**
 * 具体部件类:CPU
 * @author heiry
 *
 */
public class CPU {
  private String brands;
  public String getBrands() {
    return brands;
  }

  public void setBrands(String brands) {
    this.brands = brands;
  }

  public CPU(String brands) {
    super();
    this.brands = brands;
  }
  
}
package net.mosang.builder;
/**
 * 具体部件类:内存
 * @author heiry
 *
 */
public class Memory {
  private int capacity;

  public Memory(int capacity) {
    super();
    this.capacity = capacity;
  }

  public int getCapacity() {
    return capacity;
  }

  public void setCapacity(int capacity) {
    this.capacity = capacity;
  }
}
package net.mosang.builder;
/**
 * 具体部件类:主板
 * @author heiry
 *
 */

public class Mainboard {
  private String producer;

  public String getProducer() {
    return producer;
  }

  public void setProducer(String producer) {
    this.producer = producer;
  }

  public Mainboard(String producer) {
    super();
    this.producer = producer;
  }
}
package net.mosang.builder;
/**
 * 客户端调用类
 * @author heiry
 *
 */
public class Client {

  public static void main(String[] args) {
    MosangSuperComputer msc = new MosangSuperComputer();
    FoxconnDirector fd = new FoxconnDirector(msc);
    Computer myComputer = fd.directComputer();
    System.out.println(myComputer);
  }
}

输出结果

陌桑超算使用的是AMD的CPU
陌桑超算使用的是华硕主板
陌桑超算使用的是256G内存
net.mosang.builder.Computer@15db9742

 

 >>



© 2009-2024 MOSANG.NET DESIGNED BY HEIRY