您好,欢迎访问三七文档
建造者模式课后第一题:产品类: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;}}具体建造者类:publicclassPersonType1extendsPersonCreate{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();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{privatestaticSingletonWindowinstance=null;privateSingletonWindow(){super(内部窗口,true,true,true);System.out.println(创建了一个内部窗体);}publicstaticSingletonWindowgetInstance(){if(instance==null){instance=newSingletonWindow();}else{System.out.println(已经创建了一个内部窗体!);}returninstance;}}测试类: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);this.btnAdd.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventarg0){itf=SingletonWindow.getInstance();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(机器人学);bird.cry();}}测试类:publicclassTest{publicstaticvoidmain(String[]args){Birdbird=newBird();Dogdog=newDog();RobotAdapteradapter=newRobotAdapter(bird,dog);adapter.run();adapter.cry();}}组合模式课后习题一publicabstractclassMyElement{publicabstractvoideat();publicabstractvoidadd(MyElementelement);publicabstractvoidremove(MyElementelement);}publicclassAppleextendsMyElement{publicvoideat(){System.out.println(吃苹果);}publicvoidadd(MyElementelement){}publicvoidremove(MyElementelement){}}publicclassBananaextendsMyElement{publicvoideat(){System.out.println(吃香蕉);}publicvoidadd(MyElementelement){}publicvoidremove(MyElementelement){}}publicclassPearextendsMyElement{publicvoideat(){System.out.println(吃梨子);}publicvoidadd(MyElementelement){}publicvoidremove(MyElementelement){}}publicclassPlateextendsMyElement{privateArrayListlist=newArrayList();publicvoideat(){for(Objectobject:list){((MyElement)object).eat();}}publicvoidadd(MyElementelement){list.add(eleme
本文标题:设计模式课后习题
链接地址:https://www.777doc.com/doc-5930393 .html