GOF23学习笔记(四)建造模式

建造模式的本质

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

● 由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。

例子

飞船类,由轨道舱类,引擎类,逃逸舱类组成。如果不使用建造者模式,我们的客户端不仅需要实例化飞船类,还要实例化轨道舱类,引擎类,逃逸舱类,并且将它们的对象set到飞船对象里,做了非常多的操作。

package builder;

public class AirShip {
	private OrbitalModule orbitalModule; //轨道舱
	private Engine engine; //发动机
	private EscapeTower escapeTower; //逃逸舱
	public OrbitalModule getOrbitalModule() {
		return orbitalModule;
	}
	public void setOrbitalModule(OrbitalModule orbitalModule) {
		this.orbitalModule = orbitalModule;
	}
	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 OrbitalModule{
	private String name;

	public OrbitalModule(String name) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}

//引擎类
class Engine{
	private String name;

	public Engine(String name) {
		super();
		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) {
		super();
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

构建器接口以及实现类

//构建器接口
public interface AirShipBuilder {
	Engine builderEngine();
	OrbitalModule builderOrbitalModule();
	EscapeTower builderEscapeTower();
}

//实现类
public class IcyAirShipBuilder implements AirShipBuilder{

	@Override
	public Engine builderEngine() {
		System.out.println("构建发动机");
		return new Engine("发动机");
	}

	@Override
	public OrbitalModule builderOrbitalModule() {
		System.out.println("构建发动机");
		return new OrbitalModule("发动机");
	}

	@Override
	public EscapeTower builderEscapeTower() {
		System.out.println("构建逃逸塔");
		return new EscapeTower("逃逸塔");
	}

}
装配器接口以及实现类
public interface AirShipDirector {
	/**
	 * 组装飞船对象
	 * @return
	 */
	AirShip directAirship();
}


public class IcyAirshipDirector implements AirShipDirector{
	//组装器首先需要建造器帮我们构建起各个对象
	private AirShipBuilder builder;
	
	
	
	public IcyAirshipDirector(AirShipBuilder builder) {
		super();
		this.builder = builder;
	}



	@Override
	public AirShip directAirship() {
		//构建器创建对象
		Engine engine = builder.builderEngine();
		EscapeTower escapeTower =  builder.builderEscapeTower();
		OrbitalModule orbitalModule = builder.builderOrbitalModule();
		//组装器组装对象
		AirShip ship = new AirShip();
		ship.setEngine(engine);
		ship.setEscapeTower(escapeTower);
		ship.setOrbitalModule(orbitalModule);
		
		return ship;
	}

}

测试类,测试类只需要选择构建器和装配器,就能得到飞船这个复杂的对象

public class Test {
	public static void main(String[] args) {
		AirShipDirector director = new IcyAirshipDirector(new IcyAirShipBuilder());
		AirShip ship = director.directAirship();
		System.out.println(ship.getEngine());
	}
}
GOF23学习笔记(四)建造模式_第1张图片



你可能感兴趣的:(设计模式)