zoukankan      html  css  js  c++  java
  • 设计模式——建造者模式

           HeadFirst中并没有把建造者模式(生成器模式)当做常用的设计模式来讲解,只是在附录中一带而过。

           建造者模式的本质:
           分离了对象组件的单独构造(由Builder来负责)和装配(由Director)来负责。从而可以构建出复杂的对象。这个模式适用于:某个对象的构建
           过程复杂的情况先使用。由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象相同的构建器不同的装配顺序也可以

           做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。

           Demo:

          

    package com.wxisme.builder;
    
    /**
     * 飞船
     * @author Administrator
     *
     */
    public class AirShip {
    	
    	private OrbitalModule orbitalModule;  //轨道仓
    	private Engine engine; //发动机
    	private EscapeTower escapeTower;  //逃逸塔�
    	
    	
    	public void launch(){
    		System.out.println("发射!");
    	}
    	
    	
    	
    	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) {
    		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) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		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();
    	OrbitalModule builderOrbitalModule();
    	EscapeTower  builderEscapeTower();
    }
    
    
    public interface AirShipDirector {
    	
    	AirShip   directAirShip();
    	
    }
    
    
    
    public class MyAirShipBuilder implements AirShipBuilder {
    
    	//StringBuilder, 以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder
    	@Override
    	public Engine builderEngine() {
    		System.out.println("构建发动机");
    		return new Engine("MyEngine");
    	}
    
    	@Override
    	public EscapeTower builderEscapeTower() {
    		
    		System.out.println("构建逃逸塔");
    		return new EscapeTower("MyEscapeTower");
    	}
    
    	@Override
    	public OrbitalModule builderOrbitalModule() {
    		System.out.println("构建轨道仓");
    		return new OrbitalModule("MyOrbitalModule");
    	}	
    	
    }
    
    
    
    public class MyAirshipDirector implements AirShipDirector {
    
    	private AirShipBuilder builder;
    
    	public MyAirshipDirector(AirShipBuilder builder) {
    		this.builder = builder;
    	}
    
    
    	@Override
    	public AirShip directAirShip() {
    		Engine e = builder.builderEngine();
    		OrbitalModule o = builder.builderOrbitalModule();
    		EscapeTower et = builder.builderEscapeTower();
    		
    		//装配飞船对象
    		AirShip ship = new AirShip();
    		ship.setEngine(e);
    		ship.setEscapeTower(et);
    		ship.setOrbitalModule(o);
    		
    		return ship;
    	}
    
    }
    
    
    public class Client {
    	public static void main(String[] args) {
    		
    		AirShipDirector director = new MyAirshipDirector(new MyAirShipBuilder());
    
    		AirShip ship = director.directAirShip();
    		
    		System.out.println(ship.getEngine().getName());
    		
    		ship.launch();
    		
    	}
    }
    
  • 相关阅读:
    详细讲解Linux下安装python3(Python3.5.4)
    JavaScript抽象语法树英文对照
    关于MacBook Pro外接4K/60HZ显示器的问题
    Vue组件v-if新渲染的组件不更新
    Vue 子组件与子组件之间传值
    Spring整合CXF步骤,Spring实现webService,spring整合WebService
    CXF错误:Unsupported major.minor version 51.0,java.lang.UnsupportedClassVersionErro
    springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
    jquery.validate中使用remote,remote相同值不校验问题解决
    MyBatis返回主键,MyBatis Insert操作返回主键
  • 原文地址:https://www.cnblogs.com/wxisme/p/4520998.html
Copyright © 2011-2022 走看看