建造者模式

电脑用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);
	}
}
上一篇:CHAPER 14 1 3 5


下一篇:iOS技术沙龙之:Objective-C 简介