設(shè)計模式課后習(xí)題_第1頁
設(shè)計模式課后習(xí)題_第2頁
設(shè)計模式課后習(xí)題_第3頁
設(shè)計模式課后習(xí)題_第4頁
設(shè)計模式課后習(xí)題_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、建造者模式課后第一題:產(chǎn)品類: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();課后第二題:產(chǎn)品類: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內(nèi)存");具體建造類:public class Laptop extends Factory public void installCpu() c.setCpu("intel");public void installStorage() c.setStorage("1G內(nèi)存");指揮者類: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("內(nèi)部窗口",true,true,true);System.out.println("創(chuàng)建了一個內(nèi)部窗體");public static SingletonWindow getInstance()if(instance=null)instance=new SingletonWindow

11、(); elseSystem.out.println("已經(jīng)創(chuàng)建了一個內(nèi)部窗體!");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("添加一個內(nèi)部窗體");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();適配器模式課后第一題目標(biāo)抽象類: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("機(jī)器人學(xué)");dog.run();public void cry() System.ou

16、t.print("機(jī)器人學(xué)");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();組合模式課后習(xí)題一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();課后習(xí)題二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();裝飾模式課后習(xí)題一抽象構(gòu)建類:public interface Transform public void move();具體構(gòu)建類: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("變成醫(yī)生機(jī)

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("變成飛機(jī)");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();課后習(xí)題二:抽象構(gòu)建

32、類:public interface AbstractBook public void borrowBook();public void returnBook();具體構(gòu)建類: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("添加了凍結(jié)方法");publi

34、c void freeze()System.out.println("凍結(jié)方法");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();外觀模式課后習(xí)題一:子系統(tǒng)類: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();課后習(xí)題二:子系統(tǒng)類: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("內(nèi)存的自檢啟動");public class OS public void load()System.out.println("操作系統(tǒng)正在載入");外觀類: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();命令模式課后習(xí)題一:接收者類:public class Light public void open()System.out.println("打開電燈");public void close()System.out.println("關(guān)閉電燈&q

41、uot;);public class Wind public void open()System.out.println("打開風(fēng)扇");public void close()System.out.println("關(guān)閉風(fēng)扇");抽象命令類: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();調(diào)用者類: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();觀察者模式課后習(xí)題一:抽象目標(biāo)類: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);具體目標(biāo)類: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("股票價格上漲,當(dāng)前價格為:"+price+"元!");public void warn(double price) System.out.println("股票價格下跌,當(dāng)前價格為:"+price+"元!");測試類:public class Test public static void main(String args) AbStock stock=new

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論