设计模式第三次作业
1、要求:某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(向前和向后)遍历。使用Iterator模式来设计。
类图
![](https://i.imgur.com/C9tf2FU.png)
代码
abstract class AbstractProductList {
private String Name;
public abstract AbstractIterator getIterator();
}
class ProductList extends AbstractProductList{
@Override
public AbstractIterator getIterator() {
return new MyIterator(this);
}
}
public class AbstractIterator {
public void next();
public boolean isLast();
public boolean isFirst();
public String getNext();
public String getPrevious();
}
class MyIterator implements AbstractIterator{
@Override
public void next() {}
@Override
public boolean isLast() {}
@Override
public boolean isFirst() {}
@Override
public String getNext() {}
@Override
public String getPrevious() {}
}
2、要求:某公司欲开发一个基于Windows平台的公告板系统,系统提供主菜单(Menu)其中有菜单项(MenuItem)。通过Menu类的addMenuItem()方法增加菜单项。菜单项的打开方法是click(),每个菜单项包含一个抽象命令类,具体命令类包括OpenCommand()、CreateCommand()、EditCommand()等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen())的open()、create()、edit())等方法。使用Command模式来设计。
类图
![](https://i.imgur.com/cC0F4Os.png)
代码
public class MenuItem{
private AbstactCommand command;
public void click(){
command.excute();
}
public void setCommand(AbstactCommand command){
this.command=command;
}
}
public class Menu{
private ArrayList<MenuItem> menuItemList=new ArrayList();
public void addMenuItem(MenuItem menuItem){
menuItemList.add(menuItem);
}
}
public abstact class AbstractCommand{
public abstract void excute(){}
}
public class AbstractCommand extend AbstractCommand{
private BoardScreen boardScreen;
public void excute(){}
}
public class BoardScreen{
public void open(){}
public void create(){}
public void edit(){}
}
类图
![](https://i.imgur.com/wH83Cz3.png)
代码
public abstract class AbstractChatRoom {
public abstract void register(Member member);
public abstract void sendText(String from, String to, String text);
public abstract void sendImage(String from, String to, String image);
}
public class ChatGroup extends AbstractChatRoom {
private Map<String,Member> members=new Map<String,Member>();
@Override
public void register(Member member) {}
@Override
public void sendText(String goal, String root, String message) {}
@Override
public void sendImage(String from, String to, String image) {}
}
public abstract class Member {
protected String name;
protected AbstractChatRoom room;
public Member(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AbstractChatRoom getRoom() {
return room;
}
public void setRoom(AbstractChatRoom room) {
this.room = room;
}
public abstract void sendText(String goal, String message);
public abstract void sendImage(String goal, String image);
public void receiveText(String root , String message){}
public void receiveImage(String root , String image){}
}
public class DiamondMember extends Member {
public DiamondMember(String name) {
super(name);
}
@Override
public void sendText(String goal, String message) {
super.room.sendText(this.name, goal, text);
}
@Override
public void sendImage(String goal, String image) {
super.room.sendImage(this.name, goal, image);
}
}
4、要求:设计一个网上书店,对系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有15元的折扣。使用Strategy模式来设计。
类图
![](https://i.imgur.com/6Y1yORM.png)
代码
public class BookCart{
private ArrayList<BookOrder> BookList=new ArrayList<BookOrder>();
public void addBookOrder(BookOrder order){}
public void delBookOrder(BookOrder order){}
public double getbill(){}
}
public class BookOrder{
private string BookID;
private double Bookprice;
private string type;
private int amount;
private DiscountStrategy strategy;
public double getOrderMoney(){}
public void setDiscountStrategy(){}
}
public abstact class DiscountStrategy{
public abstract double(int amount,double price){}
}
public class ComputerBookDiscount exxtends DiscountStrategy{
public double count(int amount,double price){
return amount*price*0.9;
}
}