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; pub
2、lic 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 GamePe
3、rson 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 vo
4、id 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.cr
5、eateFace(); 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.getF
6、ace()+“穿着“+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
7、.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“); publi
8、c 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.install
9、Cpu(); 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 JInternalF
10、rame private static SingletonWindow instance=null; private SingletonWindow() super(“内部窗口 “,true,true,true); System.out.println(“创建了一个内部窗体 “); public static SingletonWindow getInstance() if(instance=null) instance=new SingletonWindow(); else System.out.println(“已经创建了一个内部窗体! “); return instance; 测试类:
11、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(fa
12、lse); 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(“添加一个内部窗体
13、“); 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(i
14、tf); ); 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(“鸟叫 “); 适配器类: pub
15、lic 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 mai
16、n(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);
17、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(MyElem
18、ent 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
19、)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
20、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 a
21、bstract 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 a
22、dd(AbstractFile file) public void delete(AbstractFile file) public void lookThrough() public String getfileName() return fileName; public class TextFile extends AbstractFile private String fileName; public TextFile(String fileName) this.fileName=fileName; public void add(AbstractFile file) public vo
23、id delete(AbstractFile file) public void lookThrough() public String getfileName() return fileName; public class VedioFile extends AbstractFile private 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 AbstractFile private ArrayList list=new ArrayList(); private String fileName; public Folder(String fileName) this.fileName=fileName; public void add(AbstractFile file) list.add(file);