设计模式课后习题.docx
《设计模式课后习题.docx》由会员分享,可在线阅读,更多相关《设计模式课后习题.docx(28页珍藏版)》请在冰豆网上搜索。
![设计模式课后习题.docx](https://file1.bdocx.com/fileroot1/2022-10/20/0e4d8c70-6e28-47e8-aae6-310f80b8aa72/0e4d8c70-6e28-47e8-aae6-310f80b8aa721.gif)
设计模式课后习题
建造者模式
课后第一题:
产品类:
publicclassGamePerson{
privateStringface;
privateStringgender;
privateStringcloth;
publicStringgetFace(){returnface;
}
publicvoidsetFace(Stringface){this.face=face;
}
publicStringgetGender(){
returngender;
}
publicvoidsetGender(Stringgender){this.gender=gender;
}
publicStringgetCloth(){
returncloth;
}
publicvoidsetCloth(Stringcloth){this.cloth=cloth;
}
}
抽象建造类:
publicabstractclassPersonCreate{
protectedGamePersonperson=newGamePerson();
publicabstractvoidcreateFace();
publicabstractvoidcreateGender();
publicabstractvoidcreateCloth();
publicGamePersongetPerson(){
returnperson;
具体建造者类:
publicclassPersonTypelextendsPersonCreate{
publicvoidcreateFace(){
person.setFace(”瓜子脸");
}
publicvoidcreateGender(){person.setGender("美女");
}
publicvoidcreateCloth(){
person.setCloth("洛丽塔");
}
}
具体建造类:
publicclassPersonType2extendsPersonCreate{
publicvoidcreateFace(){person.setFace("国字脸");
}
publicvoidcreateGender(){person.setGender(”帅哥");
}
publicvoidcreateCloth(){
person.setCloth("西装革履");
}
}
指挥者类:
publicclassGamePlayer{
privatePersonCreatepc;
publicvoidchoseType(PersonCreatepc){this.pc=pc;
}
publicGamePersoncreate(){pc.createCloth();pc.createFace();pc.createGender();returnpc.getPerson();
}
}
测试类:
publicclassTest{
publicstaticvoidmain(String[]args){
PersonCreatepc=newPersonType1();
GamePlayergp=newGamePlayer();gp.choseType(pc);
GamePersonperson=gp.create();
KM穿着
System.out.println(”游戏人物特征:
”);
System.out.println(”长着一张"+person.getFace()+"
"+person.getCloth()+"的"+person.getGender());
}
}
课后第二题:
产品类:
publicclassComputer{
privateStringcpu;
privateStringstorage;
publicStringgetCpu(){
returncpu;
}
publicvoidsetCpu(Stringcpu){
this.cpu=cpu;
}
publicStringgetStorage(){
returnstorage;
}
publicvoidsetStorage(Stringstorage){
this.storage=storage;
}
}
抽象建造类:
publicabstractclassFactory{
protectedComputerc=newComputer();
publicabstractvoidinstallCpu();
publicabstractvoidinstallStorage();
publicComputergetComputer(){
returnc;
}
}
具体建造者类:
publicclassDesktopextendsFactory{
publicvoidinstallCpu(){
c.setCpu("AMD");
}
publicvoidinstallStorage(){
c.setStorage("8G内存”);
}
具体建造类:
publicclassLaptopextendsFactory{
publicvoidinstallCpu(){
c.setCpu("intel");
}
publicvoidinstallStorage(){
c.setStorage("1G内存");
}
}
指挥者类:
publicclassUser{
privateFactoryf;
publicvoidbuy(Factoryf){
this.f=f;
}
publicComputercon(){
f.installCpu();
f.installStorage();
returnf.getComputer();
}
}
测试类:
publicclassTest{
publicstaticvoidmain(String[]args){
Factoryf=newLaptop();
Useru=newUser();
u.buy(f);
Computerc=u.con();
System.out.println(c.getCpu()+""+c.getStorage());
}
}
单例模式
课后第一题:
懒汉式模式:
publicclassSingletonWindowextendsJinternalFrame{
privatestaticSingletonWindowinstanee=null;
privateSingletonWindow(){
super("内部窗口",true,true,true);
System.out.println(”创建了一个内部窗体”);
publicstaticSingletonWindowgetlnstance(){
if(instanee==null){
instanee=newSingletonWindow();
}
else{
System.out.println(”已经创建了一个内部窗体!
”);
}
returninstanee;
}
}
测试类:
publicclassMainextendsJFrame{
privatestaticfinallongserialVersionUID=1L;
privateJButtonbtnAdd;
privateJPanelbtnpl;
privateJDesktopPanedtp;
privateJinternalFrameitf;
publicMain(){
this.setSize(newDimension(600,700));
this.setDefaultCloseOperation(EXIT_ON_CLOSE
this.setResizable(false);
this.setVisible(true);;
this.setLocationRelativeTo(this);
this.setTitle("实验2");
this.setLayout(null);
this.dtp=newJDesktopPane();
this.dtp.setBounds(newRectangle(0,0,600,600));
this.btnpl=newJPanel();
this.btnpl.setBounds(newRectangle(0,600,600,100));
this.btnAdd=newJButton("添加一个内部窗体”);
this.btnAdd.setBounds(newRectangle(10,10,150,30));
this.add(dtp);
this.add(btnpl);
this.btnpl.add(btnAdd);
publicvoidactionPerformed(ActionEventargO){
itf=SingletonWindow.getInstanee();
itf.setSize(200,200);
itf.setVisible(true);dtp.add(itf);
}
});
}
publicstaticvoidmain(String[]args){
newMain();
}
}
适配器模式
课后第一题
目标抽象类:
publicabstractclassRobot{
publicabstractvoidrun();
publicabstractvoidcry();
}
适配者类:
publicclassDog{
publicvoidrun(){
System.out.println(”狗跑");
}
}
publicclassBird{
publicvoidcry(){
System.out.println(”鸟叫");
}
}
适配器类:
publicclassRobotAdapterextendsRobot{
privateBirdbird;
privateDogdog;
publicRobotAdapter(Birdbird,Dogdog){
this.bird=bird;
this.dog=dog;
publicvoidrun(){
System.out.print("机器人学”);
dog.run();
}
publicvoidcry(){
System.out.print("机器人