设计模式第三次作业
1、要求:某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(向前和向后)遍历。使用Iterator模式来设计。
类图:
代码:
//抽象迭代器接口,定义向前和向后的双向遍历
public interface Iterator {
void forward();
void backward();
bollean hasNext();
object currentItem();
}
//抽象聚合类,存储对象并定义创建迭代器对象的接口
public interface Aggregate {
Iterator createIterator();
}
//具体聚合类,实现创建迭代器的接口,返回一个与具体聚合对象对应的具体迭代器实例
public class MyAggregate implements Aggregate{
private String[] name={"clothes","shoes","makeup","bags"};//名称存在一个字符串数组中
public Iterator createIterator(){
return new MyIterator();
} //返回具体迭代器实例
private class MyIterator implements Iterator{
private int currentIndex;
public void forward(){
currentIndex=name.length;
for(int i=currentIndex;i>0;i--){
System.out.println(name[i]);
currentIndex--;
}
}
public void backward(){
currentIndex=0;
for(int i=currentIndex;i<name.length;i++){
System.out.println(name[i]);
currentIndex++;
}
}
public boolean hasNext(){
return currentIndex==name.length;
}
public String currentItem(){
return name[currentIndex];
}
}
}
//具体迭代器,完成对对象的遍历,同时在进行遍历时跟踪其当前位置
public class MyIterator implements Iterator{
private Aggregate strings;
public MyIterator(MyAggregate strings){
this.strings=strings;
}
private int currentIndex;
public void forward(){}
public void backward(){}
public boolean hasNext(){}
public String currentItem(){}
}
2、要求:某公司欲开发一个基于Windows平台的公告板系统,系统提供主菜单(Menu)其中有菜单项(MenuItem)。通过Menu类的addMenuItem()方法增加菜单项。菜单项的打开方法是click(),每个菜单项包含一个抽象命令类,具体命令类包括OpenCommand()、CreateCommand()、EditCommand()等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen())的open()、create()、edit())等方法。使用Command模式来设计。
类图:
代码:
//声明执行操作的接口
public abstract class AbstractCommand{
public void excute();
}
//ConcreteCommamd调用接受者相应操作以实现execute
public abstract class MenuOpenCommand extends AbstractCommand{
private BoardScreen boardscreen;
public CreateCommand(BoardScreen boardscreen){
this.boardscreen=boardscreen;
}
//调用公告板系统界面类BoardScreen的open方法
public void excute(){
boardscreen.open();
}
}
public abstract class MenuEditCommand extends AbstractCommand{
private BoardScreen boardscreen;
public MenuEditCommand (BoardScreen boardscreen){
this.boardscreen=boardscreen;
}
//调用公告板系统界面类BoardScreen的open方法
public void excute(){
boardscreen.edit();
}
}
public abstract class MenuCreateCommand extends AbstractCommand{
private BoardScreen boardscreen;
public MenuCreateCommand (BoardScreen boardscreen){
this.boardscreen=boardscreen;
}
//调用公告板系统界面类BoardScreen的open方法
public void excute(){
boardscreen.create();
}
}
//Invoker请求的发送者
public class MenuItem{
private abstructCommand command;
public MenuItem(abstructCommand command){
this.command=command;
}
public void click(){
this.command.execute();
}
}
//Receive实施与执行一个请求相关的操作
public class Menu{
private MenuItem menuitem;
public void addMenuItem(MenuItem menuitem){
menu.add(menuitem);
}
public void clickMenuItem(){
menuitem.click();
}
}
//Client创建一个具体命令对象并设定其接受者
public class Client{
public static void main(String args[]){
Menu menu=new Menu();//菜单
BoardScreen bs = new BoardScreen();
abstructCommand oc=new MenuOpenCommand (bs );
MenuItem item1=new MenuItem(oc);
menu.addMenuItem(item1);
menu.clickMenuItem();//点击菜单
abstructCommand ec=new MenuEditCommand (bs );
MenuItem item2=new MenuItem(ec);
menu.addMenuItem(item2);
menu.clickMenuItem();//点击菜单
abstructCommand cc=new MenCreateCommand (bs );
MenuItem item3=new MenuItem(cc);
menu.addMenuItem(item3);
menu.clickMenuItem();//点击菜单
}
}
3、要求:某论坛系统欲增加一个虚拟聊天室,允许论坛会员通过该聊天室进行信息交流,普通会员(CommonMember)可以给其他会员发送文本信息,钻石会员(DiamondMember)可以给其他会员发送文本和图片信息。该聊天室可以对不雅字符进行过滤,如“TMD”等字符,还可以对发送图片大小进行控制。使用Mediator模式来设计。
类图:
代码:
//Mediator为各个colleague对象定义通信接口
public abstract class ChatRoom{
protected ArrayList members;
protected void register(Member member){
members.add(member);
}
public abstract void sendText(String sender,String text);
public abstract void sendPicture(String sender,String picture);
}
//ConcreteMediator实际保持colleague联系
public abstract class ConcreteChatRoom extends ChatRoom{
private filterDirtyWords filter;
private controlPicSize control;
public ConcreteChatRoom(filterDirtyWords filter,controlPicSize control){
this.filter=filter;
this.control=control;
}
//过滤不雅字符
public boolean filterDirtyWords(String text){
if(text.equals("TMD")){return true;}
else{return false;}
}
//对发送图片大小进行控制
public boolean controlPicSize(String picture){
if(picture.size>100){return true;}
else{return false;}
}
public void sendText(String text){
if(!filter(text)){//是健康字符才能发送
System.out.println("send text");
}
}
public void sendPicture(String picture){
if(!control(picture)){//符合大小的图片才能发送
System.out.println("send picture");
}
}
}
//Colleague为各个colleague定义公共方法
public abstract class Memeber{
protected ChatRoom chatroom;
protected String name;
public Member(String name){
this.name=name;
this.chatroom=chatroom;
}
public abstract void sendText(String Text);
public abstract void sendPicture(String Picture);
//ConcreteColleague实现定义的各种方法,包括发送文本图片、过滤一些文字和控制图片大小等
public abstract class CommonMember extends Memeber{
public CommonMember(String name){
super(name);
}
//普通会员只能发送文本
public void sendText(){
chatRoom.sendText(Text);
}
}
public abstract class DiamondMember extends Memeber{
public DiamondMember(String name){
super(name);
}
//钻石会员能发送文本和图片
public void sendText(){
chatRoom.sendText(Text);
}
public void sendPicture(){
chatRoom.sendPicture(picture);
}
}
//用户调用
public class Client {
public static void main(String[] args) {
filterDirtyWords filter=new filterDirtyWords();// 创建文本过滤器对象
controlPicSize control=new controlPicSize();// 创建图片大小控制器对象
ConcreteChatRoom room = new ConcreteChatRoom(filter, control);// 创建聊天室
CommonMember commonMember = new CommonMember("lin"); // 普通会员
room.register(commonMember);// 聊天室注册
DiamondMember diamondMember = new DiamondMember("xue");// 钻石会员
room.register(diamondMember);// 聊天室注册
commonMember.sendText("我是普通会员");//普通会员可以发送文本
diamondMember.sendPicture("pic1");// 钻石会员可以发送图片
diamondMember.sendText("TMD"); // 发送失败
diamondMember.sendPicture("pic2"); // 超过大小限制发送失败
}
}
4、要求:设计一个网上书店,对系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有15元的折扣。使用Strategy模式来设计。
类图:
代码:
//Strategy定义所有支持的算法的公共接口
public interface BookDiscount {
public abstract double calculatePrice(double price);
}
//以Strategy接口实现某具体算法
public class ComputerBookDiscount extends BookDiscount {//计算机类图书
private double price;
@Override
public double calculatePrice(double price) {
return price * 0.9;
}
}
public class LanguageBookDiscount extends BookDiscount {//语言类图书
private double price;
@Override
public double calculatePrice(double price) {
return price - 2;
}
}
public class NovelBookDiscount extends BookDiscount {//小说类图书
private double price;
@Override
public double calculatePrice(double price) {
return price-(price/100*15);
}
}
//Context 使用接口调用具体定义的算法
public class Book {//书籍类使用折扣接口调用折扣策略
private double price;
private String type;//图书类别
private BookDiscount bookdiscount;
public Book(String type,double price){
this.type=type;
this.price=price;
}
public void chooseDiscount(String type) {
if(type.equals("计算机类图书")){
this.bookdiscount=ComputerBookDiscount;
}
else if(type.equals("语言类图书")){
this.bookdiscount=LanguageBookDiscount;
}
if(type.equals("小说类图书")){
this.bookdiscount=NovelBookDiscount;
}
}
public double calculatePrice(double price) {
return bookdiscount.calculatePrice(price);
}
}
//用户调用
public class Client {
public static void main(String args[]) {
Book book = new Book();
BookDiscount bookdiscount = new ComputerBookDiscount();
BookDiscount bookdiscount = new LanguageBookDiscount();
BookDiscount bookdiscount = new NovelBookDiscount();
book.chooseDiscount(book.getType());
double price = book.getCost();
book.calculatePrice(price);
}
}