设计模式课后习题.doc

上传人:xin****828 文档编号:6566739 上传时间:2020-02-29 格式:DOC 页数:23 大小:68KB
返回 下载 相关 举报
设计模式课后习题.doc_第1页
第1页 / 共23页
设计模式课后习题.doc_第2页
第2页 / 共23页
设计模式课后习题.doc_第3页
第3页 / 共23页
点击查看更多>>
资源描述
建造者模式课后第一题:产品类: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 AbstractFileprivate String fileName;public TextFile(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 VedioFile extends AbstractFileprivate String fileName;public VedioFile(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 Folder extends AbstractFileprivate ArrayList list=new ArrayList();private String fileName;public Folder(String fileName) this.fileName=fileName;public void add(AbstractFile file) list.add(file);public void delete(AbstractFile file) list.remove(file);public void lookThrough() System.out.println(正在浏览+fileName+文件夹,有以下文件);Iterator i=list.iterator();if(!(i.hasNext()System.out.println(暂无任何文件);for (AbstractFile abstractFile : list) System.out.println(abstractFile.getfileName();public String getfileName() return fileName;测试类:public class Main public static void main(String args) / TODO Auto-generated method stubAbstractFile img1=new ImageFile(美女.jpg);AbstractFile vedio=new VedioFile(xxx.avi);AbstractFile txt=new TextFile(凡人修仙传.txt);AbstractFile folder=new Folder(娱乐);folder.add(img1);folder.add(txt);folder.add(vedio);System.out.println(-);folder.lookThrough();System.out.println(-);folder.delete(txt);folder.delete(vedio);folder.delete(img1);System.out.println(-);folder.lookThrough();装饰模式课后习题一抽象构建类:public interface Transform public void move();具体构建类:public class Car implements Transform public Car() System.out.println(变形金刚是一辆车);public void move() System.out.println(在陆地上移动);抽象装饰类:public class Changer implements Transform private Transform transform;public Changer(Transform transform) this.transform=transform;public void move() transform.move();具体装饰类:public class Doctor extends Robot public Doctor(Transform transform) super(transform);System.out.println(变成医生机器人);public void cure()System.out.println(我正在治疗);public class Robot extends Changer public Robot(Transform transform) super(transform);public void say()System.out.println(说话);public class Airplane extends Changer public Airplane(Transform transform) super(transform);System.out.println(变成飞机);public void fly()System.out.println(在空中飞翔);测试类:public class Test public static void main(String args) Transform transform;transform=new Car();Changer c=new Changer(transform);Robot r=new Robot(c);Doctor d=new Doctor(r);d.move();d.say();d.cure();课后习题二:抽象构建类:public interface AbstractBook public void borrowBook();public void returnBook();具体构建类:public class Book implements AbstractBook public void borrowBook() System.out.println(借书方法);public void returnBook() System.out.println(还书方法);抽象装饰类:public class AddFunction implements AbstractBook private AbstractBook ab;public AddFunction(AbstractBook ab) this.ab=ab;public void borrowBook() ab.borrowBook();public void returnBook() ab.returnBook();具体装饰类:public class AddFreeze extends AddFunction public AddFreeze(AbstractBook ab) super(ab);System.out.println(添加了冻结方法);public void freeze()System.out.println(冻结方法);public class AddLose extends AddFunction public AddLose(AbstractBook ab) super(ab);System.out.println(添加了遗失方法);public void lose()System.out.println(遗失方法);测试类:public class Test public static void main(String args) AbstractBook ab;ab=new Book();ab.borrowBook();ab.returnBook();System.out.println(-);AddFreeze af=new AddFreeze(ab);af.borrowBook();af.returnBook();af.freeze();System.out.println(-);AddLose al=new AddLose(ab);al.borrowBook();al.returnBook();al.lose();外观模式课后习题一:子系统类:public class FileWrite public void write()System.out.println(文件正在保存。);public class CipherMachine public void encrypt()System.out.println(文件正在被加密。);public class FileReader public void read()System.out.println(文件正在被读取。);外观类:public class EncyptFacade private FileReader fileReader;private CipherMachine cipherMachine;private FileWrite fileWrite;public EncyptFacade() fileReader=new FileReader();cipherMachine=new CipherMachine();fileWrite=new FileWrite();public void fileEncrypt()fileReader.read();cipherMachine.encrypt();fileWrite.write();测试类:public class Test public static void main(String args) EncyptFacade encyptFacade=new EncyptFacade();encyptFacade.fileEncrypt();课后习题二:子系统类:public class CPU public void run()System.out.println(CPU 开始运行);public class HardDisk public void read()System.out.println(正在读取硬盘);public class Memory public void check()System.out.println(内存的自检启动);public class OS public void load()System.out.println(操作系统正在载入);外观类:public class Mainframe private Memory memory;private CPU cpu;private HardDisk disk;private OS os;public Mainframe() memory=new Memory();cpu=new CPU();disk=new HardDisk();os=new OS();public void on() memory.check();cpu.run();disk.read();os.load();测试类:public class Test public static void main(String args) Mainframe mainframe=new Mainframe();mainframe.on();命令模式课后习题一:接收者类:public class Light public void open()System.out.println(打开电灯);public void close()System.out.println(关闭电灯);public class Wind public void open()System.out.println(打开风扇);public void close()System.out.println(关闭风扇);抽象命令类:public abstract class AbstractCommand public abstract void execute();具体命令类:public class LightCloseCommand extends AbstractCommand private Light light;public LightCloseCommand(Light light) this.light=light;public void execute() light.close();public class LightOpenCommand extends AbstractCommand private Light light;public LightOpenCommand(Light light) this.light=light;public void execute() light.open();public class WindCloseCommand extends AbstractCommand private Wind wind;public WindCloseCommand(Wind wind) this.wind=wind;public void execute() wind.close();public class WindOpenCommand extends AbstractCommand private Wind wind;public WindOpenCommand(Wind wind) this.wind=wind;public void execute() wind.open();调用者类:public class Controller private AbstractCommand openlight,closelight,openwind,closewind;public Controller(AbstractCommand openlight, AbstractCommand closelight, AbstractCommand openwind,AbstractCommand closewind) this.openlight = openlight;this.closelight = closelight;this.openwind = openwind;this.closewind = closewind;public void openlight()openlight.execute();public void closelight()closelight.execute();public void openwind()openwind.execute();public void closewind()closewind.execute();测试类:public class Test public static void main(String args) AbstractCommand openlight,closelight,openwind,closewind;Light light=new Light();Wind wind=new Wind();openlight=new LightOpenCommand(light);closelight=new LightCloseCommand(light);openwind=new WindOpenCommand(wind);closewind=new WindCloseCommand(wind);Controller controller=new Controller(openlight, closelight, openwind, closewind);controller.openlight();controller.closelight();controller.openwind();controller.closewind();观察者模式课后习题一:抽象目标类:public abstract class AbStock protected ArrayList people=new ArrayList();protected double price;public abstract void addPeople(AbPeople people);public abstract void setPrice(double price);public abstract void down(double downprice);public abstract void up(double upprice);抽象观察者类:public class Stock extends AbStock public void setPrice(double price) this.price=price;public void down(double downprice) if(downprice/price0.05)this.price=this.price-downprice;for (Object object : people) (AbPeople)object).warn(price);public void up(double upprice) if(upprice/price0.05)this.price=this.price+upprice;for (Object object : people) (AbPeople)object).getInfo(price);public void addPeople(AbPeople people) this.people.add(people);具体目标类:public abstract class AbPeople public abstract void getInfo(double price);public abstract void warn(double price);具体观察者类:public class People extends AbPeople public void getInfo(double price) System.out.println(股票价格上涨,当前价格为:+price+元!);public void warn(double price) System.out.println(股票价格下跌,当前价格为:+price+元!);测试类:public class Test public static void main(String args) AbStock stock=new Stock();AbPeople p1,p2,p3;p1=new People();p2=new People();p3=new People();stock.addPeople(p1);stock.addPeople(p2);stock.addPeople(p3);stock.setPrice(100.0);stock.up(10.0);stock.down(20);课后习题二:抽象目标类:public abstract class AbStudent protected String name;protected String dept;public abstract void update(String dept);抽象观察者类:具体目标类:public class Teacher extends AbStudent public Teacher(String name, String dept) this.name=name;this.dept=dept;public void update(String dept) this.dept=dept;System.out.println(教师姓名:+this.name+ 院系:+this.dept);public class Student extends AbStudent public Student(String name, String dept) this.name=name;this.dept=dept;public void update(String dept) this.dept=dept;System.out.println(学生 姓名:+this.name+ 院系:+this.dept);具体观察者类:测试类:模板方法模式课后习题二抽象类:public abstract class Account protected double lixi;protected double money=1000;public void searchaccount()System.out.println(正在查询用户信息);public abstract void showaccount();public abstract void jisuan();public void showlixi()System.out.println(利息是+lixi);public void process()this.searchaccount();this.showaccount();this.jisuan();this.showlixi();具体子类:public class CurrentAccount extends Account public void showaccount() System.out.println(活期账户);public void jisuan() this.lixi=this.money*0.2;public class SavingAccount extends Account public void showaccount() System.out.println(定期账户);public void jisuan() this.lixi=this.money*0.5;测试类:public class Test public static void main(String args) Account account1=new CurrentAccount();account1.process();Account account2=new SavingAccount();account2.process();
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!