• 建造者模式


    电脑用cpu,gpu,memory,hardDisk,power

    public class Computer {
    
    	private String cpu;
    	private String gpu;
    	private String memory;
    	private String hardDisk;
    	private String power;
    
    	//省略getset和tostring
    }
    
    public interface ComputerBuilder {
    	
    	public void cpu();
    	public void gpu();
    	public void memory();
    	public void hardDisk();
    	public void power();
    	
    	public Computer build();
    }
    
    public class Director {
    
    	public Computer build(ComputerBuilder builder) {
    		builder.cpu();
    		builder.gpu();
    		builder.memory();
    		builder.hardDisk();
    		builder.power();
    		return builder.build();
    	}
    }
    
    public class PickComputer1 implements ComputerBuilder{
    	
    	private Computer computer = new Computer();
    
    	@Override
    	public void cpu() {
    		computer.setCpu("cpu1");
    	}
    
    	@Override
    	public void gpu() {
    		computer.setGpu("gpu1");
    	}
    
    	@Override
    	public void memory() {
    		computer.setMemory("memory1");
    	}
    
    	@Override
    	public void hardDisk() {
    		computer.setHardDisk("hardDisk1");
    	}
    
    	@Override
    	public void power() {
    		computer.setPower("power1");
    	}
    
    	@Override
    	public Computer build() {
    		return computer;
    	}
    }
    
    public class Customer {
    
    	public static void main(String[] args) {
    		
    		Director d = new Director();
    		Computer computer = d.build(new PickComputer1());
    		System.out.println(computer);
    	}
    }
    

    汽车用车身,发动机,车门,车窗制造BMW,Ben_z

    public class Car {
    
    	private String carBody;
    	private String engine;
    	private String carDoor;
    	private String carWindow;
    	省略getset和tostring
    }
    
    public interface CarBuilder {
    
    	public void createCarBody();
    	public void createEngine();
    	public void createCarDoor();
    	public void createCarWindow();
    	public Car build();
    }
    
    public class Director {
    
    	public static Car build(CarBuilder builder){
    		builder.createCarBody();
    		builder.createEngine();
    		builder.createCarDoor();
    		builder.createCarWindow();
    		return builder.build();
    	}
    }
    
    public class BMWBuilder implements CarBuilder{
    
    	private Car car = new Car();
    	
    	@Override
    	public void createCarBody() {
    		// TODO Auto-generated method stub
    		car.setCarBody("制作宝马车身");
    	}
    
    	@Override
    	public void createEngine() {
    		// TODO Auto-generated method stub
    		car.setEngine("制作宝马发动机");
    	}
    
    	@Override
    	public void createCarDoor() {
    		// TODO Auto-generated method stub
    		car.setCarDoor("制作宝马车门");
    	}
    
    	@Override
    	public void createCarWindow() {
    		// TODO Auto-generated method stub
    		car.setCarWindow("制作宝马车窗");
    	}
    
    	@Override
    	public Car build() {
    		// TODO Auto-generated method stub
    		return car;
    	}
    
    }
    
    public class BenzBuilder implements CarBuilder{
    
    	private Car car = new Car();
    	
    	@Override
    	public void createCarBody() {
    		// TODO Auto-generated method stub
    		car.setCarBody("制作奔驰车身");
    	}
    
    	@Override
    	public void createEngine() {
    		// TODO Auto-generated method stub
    		car.setEngine("制作奔驰发动机");
    	}
    
    	@Override
    	public void createCarDoor() {
    		// TODO Auto-generated method stub
    		car.setCarDoor("制作奔驰车门");
    	}
    
    	@Override
    	public void createCarWindow() {
    		// TODO Auto-generated method stub
    		car.setCarWindow("制作奔驰车窗");
    	}
    
    	@Override
    	public Car build() {
    		// TODO Auto-generated method stub
    		return car;
    	}
    }
    
    public class CarStore {
    
    	public static void main(String[] args) {
    		
    		Car bmw = Director.build(new BMWBuilder());
    		System.out.println(bmw);
    		
    		Car benz = Director.build(new BenzBuilder());
    		System.out.println(benz);
    	}
    }
    
  • 相关阅读:
    MacOS的多重启动工具
    exchange 2003配置ASSP 反垃圾邮件
    VMWARE workstation 9 收缩虚拟硬盘
    [AX2012]在SSRS报表中获取从Menuitem传入的记录
    [AX2012]代码更改默认财务维度
    js使用模板快速填充数据
    CI框架--事务
    $().each 和表单事件的坑
    基于layerpage 前后端异步分页
    SQL 语句格式
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/14852115.html
Copyright © 2020-2023  润新知