关闭

抽象工厂(二)

353人阅读 评论(0) 收藏 举报
分类:
public interface CPUApi {
	public void caculate();
}
public interface MainBoardApi {
	  void installCPU();
}

public class AmdCpu implements CPUApi {
    private int pins;
    public AmdCpu(int pins){
    	this.pins = pins;
    }
	@Override
	public void caculate() { 
        System.out.println("AMD CPU pins = " + pins);
	} 
}

public class IntelCpu implements CPUApi { 
	private int pins = 0;
	public IntelCpu(int pins){
		this.pins = pins;
	} 
	@Override
	public void caculate() { 
		System.out.println("Intel CPU , pins = " + pins);
	} 
}

public class GAMainBoard implements MainBoardApi {
    private int cpuholes;
    public GAMainBoard(int cpuholes){
    	this.cpuholes = cpuholes;
    }
	@Override
	public void installCPU() {
		 System.out.println(" GAMainBoard cpuholes = "+ cpuholes);
	} 
}

public class MSIMainBoard implements MainBoardApi {
    private int cpuholes ;
    public MSIMainBoard(int cpuholes){
    	this.cpuholes= cpuholes;
    }
	 
	@Override
	public void installCPU() {
		 System.out.println("MSIMainBoard  cpuholes" + cpuholes);
	} 
}

public interface Factory {
     public CPUApi createCPUApi();
     public MainBoardApi createMainBoardApi();
}


public class SchemaOne implements Factory { 
	@Override
	public CPUApi createCPUApi() { 
		return new IntelCpu(1156);
	} 
	@Override
	public MainBoardApi createMainBoardApi() { 
		return new GAMainBoard(1156);
	} 
}

public class SchemaTwo implements Factory{ 
	@Override
	public CPUApi createCPUApi() { 
		return new AmdCpu(939);
	} 
	@Override
	public MainBoardApi createMainBoardApi() { 
		return new MSIMainBoard(939);
	} 
}

public class Engineer {
     private CPUApi cpu;
     private MainBoardApi mainBoard;
     
     public void makeComputer(Factory schema){
    	 this.cpu = schema.createCPUApi();
    	 this.mainBoard = schema.createMainBoardApi();
    	 this.cpu.caculate();
    	 this.mainBoard.installCPU();
     }
}

public class Client { 
	public static void main(String[] args) {
		 Engineer engi = new Engineer();
		 SchemaOne schema = new SchemaOne();
		 engi.makeComputer(schema);
	} 
}












0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:44732次
    • 积分:671
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:121篇
    • 译文:0篇
    • 评论:0条