码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之GOF23建造者模式

时间:2019-08-09 14:01:21      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:turn   print   info   int   alt   his   建造者模式   设计   rri   

组件很多,装配顺序不定

本质

1,分离了对象子组件的单独构造(Builder负责)和装配(Director负责),从而可以构造出复杂的对象,这个模式适用于某个对象的构建过程复杂的情况下使用

2,实现了构建和装配的解耦

/**建造者用来得到各个组件
 * @author 小帆敲代码
 *
 */
public interface AirshipBuilder {
  public Engine getngine();//可用工厂或者单例模式得到对应的组件
  public EscapeTower getEscapeTower();
  public ObitalMoudle getObitalMoudle();
}

public class FAirshipBuilder implements AirshipBuilder{
 @Override
 public Engine getngine() {
  System.out.println("建造发动机");
  return new Engine("F牌发动机");
 }
 @Override
 public EscapeTower getEscapeTower() {
  System.out.println("建造逃生塔");
  return new EscapeTower("F牌");
 }
 @Override
 public ObitalMoudle getObitalMoudle() {
  System.out.println("建造轨道舱");
  return new ObitalMoudle("F牌轨道舱");
 }
}
/**
 * 装配组件
 * @author 小帆敲代码
 *
 */
public interface AirshipDirector {
 //装配
  public Airship directAirship();
}
public class FAirshipDirector implements AirshipDirector{
 private AirshipBuilder builder;
 public FAirshipDirector(AirshipBuilder builder) {
  this.builder = builder;
 }
 @Override
 public Airship directAirship() {
  Engine e=builder.getngine();
  EscapeTower et=builder.getEscapeTower();
  ObitalMoudle o=builder.getObitalMoudle();
  
  //装配
  Airship airship=new Airship();
  airship.setEngine(e);
  airship.setEscapeTower(et);
  airship.setObtalMoudle(o);
  return airship;
 }
}
public class Airship {
  private Engine engine;//发动机
  private ObitalMoudle obtalMoudle;//轨道舱
  private EscapeTower escapeTower;//逃生塔
  
  public void lauch() {
   System.out.println("发射!!!");
  }
  
  public Engine getEngine() {
   return engine;
  }
  public void setEngine(Engine engine) {
   this.engine = engine;
  }
  public ObitalMoudle getObtalMoudle() {
   return obtalMoudle;
  }
  public void setObtalMoudle(ObitalMoudle obtalMoudle) {
   this.obtalMoudle = obtalMoudle;
  }
  public EscapeTower getEscapeTower() {
   return escapeTower;
  }
  public void setEscapeTower(EscapeTower escapeTower) {
   this.escapeTower = escapeTower;
  }
  
  
}
class Engine{
 private String name;
 public Engine(String name) {
  this.name = name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
class ObitalMoudle{
 private String name;
 public ObitalMoudle(String name) {
  this.name = name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
class EscapeTower{
 private String name;
 public EscapeTower(String name) {
  this.name = name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
public class Client {
 public static void main(String[] args) {
   FAirshipDirector f=new FAirshipDirector(new FAirshipBuilder());
   Airship airship=f.directAirship();
   
   System.out.println(airship.getEngine().getName());
   
   airship.lauch();
 }
}技术图片

设计模式之GOF23建造者模式

标签:turn   print   info   int   alt   his   建造者模式   设计   rri   

原文地址:https://www.cnblogs.com/code-fun/p/11326473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!