设计模式课后习题(DOC)

发布时间:2018-09-18 10:50:43   来源:文档文库   
字号:

建造者模式

课后第一题:

产品类:

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();

}

else{

System.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 List list=new List();

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 AbstractFile{

private 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 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 List list=new List();

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 stub

AbstractFile 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 List people=new List();

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/price>0.05){

this.price=this.price-downprice;

for (Object object : people) {

((AbPeople)object).warn(price);

}

}

}

public void up(double upprice) {

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 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();

}

}


本文来源:https://www.2haoxitong.net/k/doc/76c460a67d1cfad6195f312b3169a4517723e52c.html

《设计模式课后习题(DOC).doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式