package test5;

public abstract class Builder {

Computer computer = new Computer();

abstract public void buildZhuJi();

abstract public void buildYinPan();

abstract public void buildCpu();

abstract public void buildNeiCun();

public Computer getComputer(){

return computer;

}

}

package test5;

public class Computer {

private String cpu;
private String neiCun;
private String yinPan;
private String zhuJi;

public String getZhuJi() {

return zhuJi;

}

public void setZhuJi(String zhuJi) {

this.zhuJi = zhuJi;

}

public String getYinPan() {

return yinPan;

}

public void setYinPan(String yinPan) {

this.yinPan = yinPan;

}

public String getNeiCun() {

return neiCun;

}

public void setNeiCun(String neiCun) {

this.neiCun = neiCun;

}

public String getCpu() {

return cpu;

}

public void setCpu(String cpu) {

this.cpu = cpu;

}

}

 

package test5;

public class Factory {

private Builder computerKind;

public void setCompture(Builder kind) {

this.computerKind = kind;

}

public Computer construct(){

computerKind.buildCpu();

computerKind.buildYinPan();

computerKind.buildZhuJi();

computerKind.buildNeiCun();

return computerKind.getComputer();

}

}

package test5;

public class LapBuilder extends Builder{

@Override

public void buildZhuJi() {

computer.setZhuJi("LZJ");

}

 

@Override

public void buildYinPan() {

computer.setYinPan("LYP");

}

 

@Override

public void buildCpu() {

computer.setCpu("LC");

}

 

@Override

public void buildNeiCun() {

computer.setNeiCun("LNC");

}

}

package test5;

public class Store {

public static void main(String[] args) {

Builder lap;

Factory factory=new Factory();

for (int i = 0; i < 2; i++) {

if(i==1)

lap = new LapBuilder();

else

lap=new TaiShiBuilder();

factory.setCompture(lap);

factory.construct();

System.out.println(lap.getComputer().getCpu());

System.out.println(lap.getComputer().getNeiCun());

System.out.println(lap.getComputer().getYinPan());

System.out.println(lap.getComputer().getZhuJi());

}

 

}

}

package test5;

public class TaiShiBuilder extends Builder{

 

@Override

public void buildZhuJi() {

computer.setZhuJi("TZJ");

}

 

@Override

public void buildYinPan() {

computer.setYinPan("TYP");

}

 

@Override

public void buildCpu() {

computer.setCpu("TC");

}

 

@Override

public void buildNeiCun() {

computer.setNeiCun("TNC");

}

}

posted on 2023-10-30 23:00  夜的第七章i  阅读(2)  评论(0编辑  收藏  举报