首页 > 其他 > 详细

design_model(3)builder

时间:2018-08-29 00:32:59      阅读:210      评论:0      收藏:0      [点我收藏+]

1.构造者模式

优点:实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。

缺点:非常复杂,有很多步骤需要处理

技术分享图片

2.模式实例

public class Car {
	private  Engine  engine;
	private  Body   body;
	private  Wheel   wheel;
	public Engine getEngine() {
		return engine;
	}
	public void setEngine(Engine engine) {
		this.engine = engine;
	}
	public Body getBody() {
		return body;
	}
	public void setBody(Body body) {
		this.body = body;
	}
	public Wheel getWheel() {
		return wheel;
	}
	public void setWheel(Wheel wheel) {
		this.wheel = wheel;
	}
}

public class Wheel {
     
}

class  Body {
    
}

class  Engine{
	
}

public interface Builder {
	public abstract Wheel   createWheel();
	public abstract  Body    createBody();
	public  abstract  Engine   createEngine();
}

public class SxtBuilder implements Builder {
	private static SxtBuilder sxtBuilder = new SxtBuilder();

	@Override
	public Wheel createWheel() {
		return new Wheel();
	}

	@Override
	public Body createBody() {
		return new Body();
	}

	@Override
	public Engine createEngine() {
		return new Engine();
	}

	private SxtBuilder() {
		super();
	}

	public static SxtBuilder getSxtBuilder() {
		return sxtBuilder;
	}
}

public class SxtDerictor {
    public  static Car getCar() {
    	Car car = new  Car();
    	SxtBuilder sixBuilder = SxtBuilder.getSxtBuilder();
    	car.setEngine(sixBuilder.createEngine());
    	car.setWheel(sixBuilder.createWheel());
    	car.setBody(sixBuilder.createBody());
    	return  car;
    }
}

public class Client {
    public static void main(String[] args) {
		 Car car = SxtDerictor.getCar();
	}
}

 

design_model(3)builder

原文:https://www.cnblogs.com/gg128/p/9551769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!