设计模式课后习题(DOC)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
建造者模式
课后第一题:
产品类:
public class GamePerson {
private String face;
private String gender;
private String cloth;
public String getFace() {
return face;
}
public void setFace(String face) {
this.face = face;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCloth() {
return cloth;
}
public void setCloth(String cloth) {
this.cloth = cloth;
}
}
抽象建造类:
public abstract class PersonCreate {
protected GamePerson person=new GamePerson();
public abstract void createFace();
public abstract void createGender();
public abstract void createCloth();
public GamePerson getPerson(){
return person;
}
}
具体建造者类:
public class PersonType1 extends PersonCreate { public void createFace() {
person.setFace("瓜子脸");
}
public void createGender() {
person.setGender("美女");
}
public void createCloth() {
person.setCloth("洛丽塔");
}
}
具体建造类:
public class PersonType2 extends PersonCreate { public void createFace() {
person.setFace("国字脸");
}
public void createGender() {
person.setGender("帅哥");
}
public void createCloth() {
person.setCloth("西装革履");
}
}
指挥者类:
public class GamePlayer {
private PersonCreate pc;
public void choseType(PersonCreate pc){ this.pc=pc;
}
public GamePerson create(){
pc.createCloth();
pc.createFace();
pc.createGender();
return pc.getPerson();
}
}
测试类:
public class Test {
public static void main(String[] args) { PersonCreate pc=new PersonType1();
GamePlayer gp=new GamePlayer();
gp.choseType(pc);
GamePerson person=gp.create();
System.out.println("游戏人物特征:");
System.out.println("长着一张"+person.getFace()+"穿着"+person.getCloth()+"的"+person.getGender());
}
}
课后第二题:
产品类:
public class Computer {
private String cpu;
private String storage;
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getStorage() {
return storage;
}
public void setStorage(String storage) {
this.storage = storage;
}
}
抽象建造类:
public abstract class Factory {
protected Computer c=new Computer();
public abstract void installCpu();
public abstract void installStorage();
public Computer getComputer(){
return c;
}
}
具体建造者类:
public class Desktop extends Factory {
public void installCpu() {
c.setCpu("AMD");
}
public void installStorage() {