




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、建造者模式课后第一题:产品类: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
2、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()re
3、turn 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
4、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
5、.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
6、("长着一张"+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;pub
7、lic 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 ins
8、tallCpu() 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 v
9、oid 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();单例模式课后第一
10、题:懒汉式模式: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
11、(); 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)
12、;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(
13、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
14、.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
15、 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.ou
16、t.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
17、 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.ou
18、t.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
19、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,obj
20、2,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.ad
21、d(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;publi
22、c 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.f
23、ileName=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(
24、AbstractFile file) public void delete(AbstractFile file) public void lookThrough() public String getfileName() return fileName;public class Folder extends AbstractFileprivate ArrayList<AbstractFile> list=new ArrayList<AbstractFile>();private String fileName;public Folder(String fileName)
25、 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("暂无任何文件&q
26、uot;);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=ne
27、w 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.d
28、elete(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("在陆地上移动&qu
29、ot;);抽象装饰类: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("变成医生机
30、器人");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);Syst
31、em.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();课后习题二:抽象构建
32、类: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 Abst
33、ractBook 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("添加了冻结方法");publi
34、c 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) AbstractBoo
35、k 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
36、 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
37、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(Str
38、ing 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.pri
39、ntln("内存的自检启动");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 o
40、n() 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("关闭电灯&q
41、uot;);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 Li
42、ghtCloseCommand(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 p
43、rivate 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 Abstrac
44、tCommand 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 openli
45、ght()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 wi
46、nd=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();controll
47、er.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 abstrac
48、t void up(double upprice);抽象观察者类:public class Stock extends AbStock public void setPrice(double price) this.price=price;public void down(double downprice) if(downprice/price>0.05)this.price=this.price-downprice;for (Object object : people) (AbPeople)object).warn(price);public void up(double uppri
49、ce) if(upprice/price>0.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 pri
50、ce);具体观察者类: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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 卖车转让合同范本
- 一年级新生入学家长会的发言稿
- 《雨》阅读理解训练题及答案
- 东南亚企业合同范本
- 《长方形和正方形的周长》教学反思
- 取暖煤炉购销合同范例
- 买卖电梯合同范本
- 下定金合同范本
- 借款电子合同范本
- 农村酒店招租合同范例
- 2025新人教版英语七年级下单词英译汉默写表(小学部分)
- 《联合国教科文:学生人工智能能力框架》-中文版
- 2023年部编人教版六年级道德与法治下册全册课件【完整版】
- GB/T 34376-2017数控板料折弯机技术条件
- GB/T 22492-2008大豆肽粉
- 四年级下册美术课件 4纸卷魔术|苏少版
- 三年级下册竖式脱式计算
- 《财务风险的识别与评估管理国内外文献综述》
- 海口市存量房买卖合同模板(范本)
- 经典文学作品中的女性形象研究外文文献翻译2016年
- 高炉煤气安全知识的培训
评论
0/150
提交评论