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() r
3、eturn 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.setF
4、ace( 国字脸 );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 p
5、c.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.getGende
6、r(); 课后第二题:产品类: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 p
7、rotected 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 Lapt
8、op 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
9、 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 ,
10、true , true );System. out.println(创建了一个内部窗体 );public static SingletonWindow getInstance()if ( instance =null ) instance =new SingletonWindow();else System. out .println( 已经创建了一个内部窗体! ); return instance ;测试类:public class Main extends JFrame privatestatic finallongserialVersionUID = 1L;private JButton
11、 btnAdd ;privateJPanel btnpl ;privateJDesktopPanedtp ;privateJInternalFrameitf;public Main() this .setSize( new Dimension(600, 700);this .setDefaultCloseOperation(EXIT_ON_CLOS);Ethis .setResizable( false );this .setVisible( true );this .setLocationRelativeTo( this );this .setTitle( 实验 2);this .setLa
12、yout( 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 .
13、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();适配器模式课后第
14、一题目标抽象类: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
15、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 RobotAdapt
16、er(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(My
17、Element 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 voi
18、d 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)
19、 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(
20、);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(Abstract );public abstract void delete(Abstract);public abstract void lookThrough(); public abstract String ge
21、t();public class Image AbstractFile private String ;public Image ) this.;public void add(Abstract) public void delete(Abstract) public void lookThrough() public String get() return ;public class Text AbstractFileprivate String ;public Text ) this.;public void add(Abstract) public void delete(Abstrac
22、t) public void lookThrough() public String get() return ;public class Vedio AbstractFileprivate String ;public Vedio ) this.;public void add(Abstract) public void delete(Abstract) public void lookThrough() public String get() return ;public class Folder extends AbstractFileprivate ArrayList list=new
23、 ArrayList();private String ;public Folder(String ) this.;public void add(Abstract) list.add(file);public void delete(Abstract) list.remove(file);public void lookThrough() System.out.println( 正在浏览 + 文件夹 , 有以下文件);Iterator i=list.iterator();if(!(i.hasNext()System.out.println( 暂无任何文件);for (Abstract : l
24、ist) System.out.println(abstract();public String get() return ;测试类:public class Main public static void main(String args) / TODO Auto-generated method stubAbstract ImageFile( 美女 .jpg);Abstract VedioFile(xxx.avi);Abstract TextFile( 凡人修仙传.txt);Abstract Folder( 娱乐 );folder.add(img1);folder.add(txt);fol
25、der.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() Sy
26、stem.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 Docto
27、r(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(T
28、ransform 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
29、();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 Ab
30、stractBook 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 fr
31、eeze() 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.
32、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 public void write()System.out.println(文件正在保存。 。 );public class CipherMac
33、hine public void encrypt()System.out.println( 文件正在被加密。 );public class public void read()System.out.println(外观类:public class EncyptFacade 文件正在被读取。 );private ;private CipherMachine cipherMachine;private ;public EncyptFacade() ();cipherMachine=new CipherMachine();();public void ()();cipherMachine.encry
34、pt();();测试类:public class Test public static void main(String args) EncyptFacade encyptFacade=new EncyptFacade(); encyptFacade.();课后习题二:子系统类:public class CPU public void run()System.out.println(CPUpublic class HardDisk public void read()System.out.println(public class Memory public void check()System
35、.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() m
36、emory.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
37、 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=lig
38、ht;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(Wi
39、nd 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,clos
40、ewind;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 closel
41、ight()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 LightOpenComman
42、d(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(do