设计模式课后习题.docx

上传人:b****1 文档编号:1676201 上传时间:2022-10-23 格式:DOCX 页数:28 大小:47.48KB
下载 相关 举报
设计模式课后习题.docx_第1页
第1页 / 共28页
设计模式课后习题.docx_第2页
第2页 / 共28页
设计模式课后习题.docx_第3页
第3页 / 共28页
设计模式课后习题.docx_第4页
第4页 / 共28页
设计模式课后习题.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

设计模式课后习题.docx

《设计模式课后习题.docx》由会员分享,可在线阅读,更多相关《设计模式课后习题.docx(28页珍藏版)》请在冰豆网上搜索。

设计模式课后习题.docx

设计模式课后习题

建造者模式

课后第一题:

产品类:

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("机器人

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 公共行政管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1