关于Java 设计模式 建造者模式 Builder

关于Java 设计模式 建造者模式 Builder

1、CarModel.java

package com.ssm.cts.pattern.builder;

import java.util.ArrayList;

public abstract class CarModel {

  // 执行顺序
  private ArrayList<String> sequence = new ArrayList<String>();

  // 启动
  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 < this.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;
  }
}

2、BenzModel.java

package com.ssm.cts.pattern.builder;

public class BenzModel extends CarModel {

  @Override
  protected void alarm() {
    System.out.println("奔驰:喇叭声音...");
  }

  @Override
  protected void engineBoom() {
    System.out.println("奔驰:引擎音声音...");
  }

  @Override
  protected void start() {
    System.out.println("奔驰:启动...");
  }

  @Override
  protected void stop() {
    System.out.println("奔驰:停止");
  }

}

3、BMWModel.java

package com.ssm.cts.pattern.builder;

public class BMWModel extends CarModel {

  @Override
  protected void alarm() {
    System.out.println("宝马:喇叭声音...");
  }

  @Override
  protected void engineBoom() {
    System.out.println("宝马:引擎音声音...");
  }

  @Override
  protected void start() {
    System.out.println("宝马:启动...");
  }

  @Override
  protected void stop() {
    System.out.println("宝马:停止");
  }

}

 

4、BenzBuilder.java

package com.ssm.cts.pattern.builder;

import java.util.ArrayList;

public class BenzBuilder extends CarBuilder {
  private BenzModel benz = new BenzModel();

  @Override
  public CarModel getCarModel() {
    return this.benz;
  }

  @Override
  public void setSequence(ArrayList<String> sequence) {
    this.benz.setSequence(sequence);
  }

}

5、BMWBuilder.java

package com.ssm.cts.pattern.builder;

import java.util.ArrayList;

public class BMWBuilder extends CarBuilder {
  private BMWModel bmw = new BMWModel();

  @Override
  public CarModel getCarModel() {
    return this.bmw;
  }

  @Override
  public void setSequence(ArrayList<String> sequence) {
    this.bmw.setSequence(sequence);
  }

}

6、CarBuilder.java

package com.ssm.cts.pattern.builder;

import java.util.ArrayList;

public abstract class CarBuilder {

  // 提供组装顺序,建造一个模型。
  public abstract void setSequence(ArrayList<String> sequence);

  // 获取模型
  public abstract CarModel getCarModel();
}

7、Director.java

package com.ssm.cts.pattern.builder;

import java.util.ArrayList;

public class Director {
  @SuppressWarnings({ "unchecked", "rawtypes" })
  private ArrayList<String> sequence = new ArrayList();
  private BenzBuilder benzBuilder = new BenzBuilder();
  private BMWBuilder bmwBuilder = new BMWBuilder();

  // A类型的奔驰车
  public BenzModel getBenzModelA() {
    // 清理场景
    this.sequence.clear();

    this.sequence.add("start");
    this.sequence.add("stop");

    this.benzBuilder.setSequence(this.sequence);
    
    return (BenzModel) this.benzBuilder.getCarModel();

  }

  // C型号的宝马车
  public BMWModel getBMWModelC() {
    this.sequence.clear();

    this.sequence.add("alarm");
    this.sequence.add("start");
    this.sequence.add("stop");

    this.bmwBuilder.setSequence(this.sequence);
    return (BMWModel) this.bmwBuilder.getCarModel();
  }

}

8、Demo.java

package com.ssm.cts.pattern.builder;

public class Demo {

  // 测试
  public static void main(String[] args) {
    Director director = new Director();

    director.getBenzModelA().run();

    director.getBMWModelC().run();
  }
}

 

发表回复

您的电子邮箱地址不会被公开。