设计模式课后习题_第1页
设计模式课后习题_第2页
设计模式课后习题_第3页
设计模式课后习题_第4页
设计模式课后习题_第5页
已阅读5页,还剩18页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

建造者模式课后第一题:产品类: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() c.setStorage(“8G内存“);具体建造类:public class Laptop extends Factory public void installCpu() c.setCpu(“intel“);public void installStorage() c.setStorage(“1G内存“);指挥者类:public class User private Factory f;public void buy(Factory f)this.f=f;public Computer con()f.installCpu();f.installStorage();return f.getComputer();测试类:public class Test public static void main(String args) Factory f=new Laptop();User u=new User();u.buy(f);Computer c=u.con();System.out.println(c.getCpu()+“ “+c.getStorage();单例模式课后第一题:懒汉式模式:public class SingletonWindow extends JInternalFrame private static SingletonWindow instance=null;private SingletonWindow() super(“内部窗口“,true,true,true);System.out.println(“创建了一个内部窗体“);public static SingletonWindow getInstance()if(instance=null)instance=new SingletonWindow();elseSystem.out.println(“已经创建了一个内部窗体!“);return instance;测试类:public class Main extends JFrame private static final long serialVersionUID = 1L;private JButton btnAdd;private JPanel btnpl;private JDesktopPane dtp;private JInternalFrame itf;public Main() this.setSize(new Dimension(600, 700);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);this.setLocationRelativeTo(this);this.setTitle(“实验 2“);this.setLayout(null);this.dtp=new JDesktopPane();this.dtp.setBounds(new Rectangle(0, 0, 600, 600);this.btnpl=new JPanel();this.btnpl.setBounds(new Rectangle(0, 600, 600, 100);this.btnAdd=new JButton(“添加一个内部窗体“);this.btnAdd.setBounds(new Rectangle(10, 10, 150, 30);this.add(dtp);this.add(btnpl);this.btnpl.add(btnAdd);this.btnAdd.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) itf=SingletonWindow.getInstance();itf.setSize(200, 200);itf.setVisible(true);dtp.add(itf););public static void main(String args) new Main();适配器模式课后第一题目标抽象类:public abstract class Robot public abstract void run();public abstract void cry();适配者类:public class Dog public void run()System.out.println(“狗跑“);public class Bird public void cry()System.out.println(“鸟叫“);适配器类:public class RobotAdapter extends Robot private Bird bird;private Dog dog;public RobotAdapter(Bird bird,Dog dog) this.bird=bird;this.dog=dog;public void run() System.out.print(“机器人学“);dog.run();public void cry() System.out.print(“机器人学“);bird.cry();测试类:public class Test public static void main(String args) Bird bird=new Bird();Dog dog=new Dog();RobotAdapter adapter=new RobotAdapter(bird, dog);adapter.run();adapter.cry();组合模式课后习题一public abstract class MyElement public abstract void eat();public abstract void add(MyElement element);public abstract void remove(MyElement element);public class Apple extends MyElement public void eat() System.out.println(“吃苹果“);public void add(MyElement element) public void remove(MyElement element) public class Banana extends MyElement public void eat() System.out.println(“吃香蕉“);public void add(MyElement element) public void remove(MyElement element) public class Pear extends MyElement public void eat() System.out.println(“吃梨子“);public void add(MyElement element) public void remove(MyElement element) public class Plate extends MyElement private ArrayList list=new ArrayList();public void eat() for (Object object : list) (MyElement)object).eat();public void add(MyElement element) list.add(element);public void remove(MyElement element) list.remove(element);测试类:public class Client public static void main(String args) MyElement obj1,obj2,obj3,obj4,obj5;Plate plate1,plate2,plate3;obj1=new Apple();obj2=new Pear();obj3=new Banana();plate1=new Plate();plate1.add(obj1);plate1.add(obj2);plate1.add(obj3);obj4=new Apple();obj5=new Banana();plate2=new Plate();plate3=new Plate();plate2.add(obj4);plate2.add(obj5);plate3.add(plate1);plate3.add(plate2);plate3.eat();课后习题二public abstract class AbstractFile public abstract void add(AbstractFile file );public abstract void delete(AbstractFile file);public abstract void lookThrough();public abstract String getfileName();public class ImageFile extends AbstractFile private String fileName;public ImageFile(String fileName) this.fileName=fileName;public void add(AbstractFile file) public void delete(AbstractFile file) public void lookThrough() public String getfileName() return fileName;public class TextFile extends AbstractFilep

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论