zoukankan      html  css  js  c++  java
  • 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();
    	}
    }
    
  • 相关阅读:
    Thinkphp 模板中使用自定义函数的方法
    thinkphp 邮件发送
    str_replace使用
    SQL备份一张表的数据
    error: Allowed memory size
    LitJson使用
    implode,explode的使用
    ModelState.AddModelError使用
    HTTP 错误 404.2
    验证码显示不出来,在THINKPHP中的使用
  • 原文地址:https://www.cnblogs.com/gg128/p/9551769.html
Copyright © 2011-2022 走看看