欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java建造者模式

程序员文章站 2022-04-05 11:29:28
建造者模式 建造者模式适用场景: 建造一个复杂的对象适用,将构建对象的过程分开,每个类单独构造对象的一部分,最后组装起来,返回我们需要的对象。 下面的例子主要讲解构造一个飞船 Demo: //要获得的对象,但是各个组件要拆分开,让对应的类去实现 class AirShip { private Orb ......

建造者模式

建造者模式适用场景:

  建造一个复杂的对象适用,将构建对象的过程分开,每个类单独构造对象的一部分,最后组装起来,返回我们需要的对象。

下面的例子主要讲解构造一个飞船

demo: 

//要获得的对象,但是各个组件要拆分开,让对应的类去实现

class airship {
  private orbitamodule orbitamodule;//轨道舱
  private engine engine; //发动机
  private escapetower escapetower;//逃逸塔
  public orbitamodule getorbitamodule() {
    return orbitamodule;
  }
  public void setorbitamodule(orbitamodule orbitamodule) {
    this.orbitamodule = orbitamodule;
  }
  public engine getengine() {
    return engine;
  }
  public void setengine(engine engine) {
    this.engine = engine;
  }
  public escapetower getescapetower() {
    return escapetower;
  }
  public void setescapetower(escapetower escapetower) {
    this.escapetower = escapetower;
  }
}

class orbitamodule{

  private string name;

  public string getname() {

    return name;

  }

  public void setname(string name) {

    this.name = name;

  }

  public orbitamodule(string name) {

    super();

  this.name = name;

  }

}

class engine{

  private string name;

  public string getname() {

    return name;

  }

  public void setname(string name) {

    this.name = name;

  }

  public engine(string name) {

    super();

    this.name = name;

  }

}

class escapetower{

  private string name;

  public string getname() {

    return name;

  }

  public void setname(string name) {

    this.name = name;

  }

  public escapetower(string name) {

    super();

    this.name = name;

  } 

}

 

public interface airshipbuilder {

 

  engine builderengine();
  orbitamodule builderorbitamodule();
  escapetower builderescapetower();
}

//创建返回对象的接口

public interface airshipdirector {
  airship directairship();
}

//组装对象的实现类

public class airshipbuilderimpl implements airshipbuilder{

  @override
  public engine builderengine() {
    system.out.println("构建发动机");
    return new engine("发动机");
  }

  @override
  public orbitamodule builderorbitamodule() {
    system.out.println("构建逃逸塔");
    return new orbitamodule("逃逸塔");
  }

  @override
  public escapetower builderescapetower() {
    system.out.println("构建轨道舱");
    return new escapetower("轨道舱");
  }
}

//返回对象的实现类

public class airshipdirectorimpl implements airshipdirector{

  //创建一个构造组件的接口对象,此处可以使用工厂模式进行修改

  private airshipbuilder airshipbuilder;

  public airshipdirectorimpl(airshipbuilder airshipbuilder) {
    super();
    this.airshipbuilder = airshipbuilder;
  }
  @override
  public airship directairship() {
    engine builderengine = airshipbuilder.builderengine();
    orbitamodule builderorbitamodule = airshipbuilder.builderorbitamodule();
    escapetower builderescapetower = airshipbuilder.builderescapetower();
    airship ship = new airship();
    ship.setengine(builderengine);
    ship.setescapetower(builderescapetower);
    ship.setescapetower(builderescapetower);
    return ship;
  }
}

//客户端调用

public class client {

  public static void main(string[] args) {
    airshipdirector airshipdirector = new airshipdirectorimpl(new airshipbuilderimpl());
    airship directairship = airshipdirector.directairship();
    system.out.println(directairship);
  }
}

建造者模式主要就是将建造飞船的过程简单的拆分开,让构建飞船的不同的组件让不同的类去完成,此处就可以和工厂模式和单例模式相结合,让开发变得更加清晰。