design_model(3)builder
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(); } }