设计模式第三次作业

1、要求:某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(向前和向后)遍历。使用Iterator模式来设计。
类图:

代码:


Public interface Iterator{
  Void getnext();
  Void getforward();
  Bollean isDone();
  Object getcurrent();
}

Public interface Aggregate{
  Interator contextInterface();
}

Public class concreteAggregate implements Aggregate{
   Private string[] storename={“food”,’’bag’’,’’vege’’};
   Public Iterator contextInterface(){
     Return new MyIterator();}
  Private class ConcreteIterator implements Iterator{
Private int nowindex;
Public Void getnext()
{
   Nowindex=0;
   For(i=0;i<storename.length;i++)
{
      System.out.printlin(storename[i]);
Nowindex++;
}
}
Public Void getforward()
{
   Nowindex=storename.length;
   For(i=Nowindex;i>0;i--)
{
      System.out.printlin(storename[i]);
Nowindex---;
}
}
Public Bollean isDone()
{
If(nowindex==storename.length)
Return false;
Else retuen true;
}
Public string getcurrent(){
  Return storename[nowindex];
}
}
}

Public class concreteIterator implements Iterator{

Pubic Void getnext();
  Public Void getforward();
  Public Bollean isDone();
  Public Object getcurrent();


}

2、要求:某公司欲开发一个基于Windows平台的公告板系统,系统提供主菜单(Menu)其中有菜单项(MenuItem)。通过Menu类的addMenuItem()方法增加菜单项。菜单项的打开方法是click(),每个菜单项包含一个抽象命令类,具体命令类包括OpenCommand()、CreateCommand()、EditCommand()等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen())的open()、create()、edit())等方法。使用Command模式来设计。
类图:

代码:

Public  void Menu{
private arraylist <MenuItem> menulistitem=new arraylist <MenuItem> menulistitem();
Public void addMenu(menulistitem ltem){
 This.menulistitem.add(item);
}
Public void clickMenu()
{
Menuitem.click();
}

Public abstract class Command{
Public void excute();
}
Public class OPenCommand extends Command{
  Private BoardScreen boardscreen;
Public void excute();
}
Public class EditeCommand extends Command{
  Private BoardScreen boardscreen;
Public void excute();
}
Public class CreateCommand extends Command{
  Private BoardScreen boardscreen;
Public void excute();
}
Public class BoardScreen(){
 Public void open(){};
Public void create(){};
Public void edit(){};
}
Public class MenuItem{
Private Command commandl
public void click(){
This.command.excute();
}
}
Public class client{
MenuITem menuitem=new MenuITem();
Menu.addMenu(menuitem);
Menuitem.clickMenu();
}

3、要求:某论坛系统欲增加一个虚拟聊天室,允许论坛会员通过该聊天室进行信息交流,普通会员(CommonMember)可以给其他会员发送文本信息,钻石会员(DiamondMember)可以给其他会员发送文本和图片信息。该聊天室可以对不雅字符进行过滤,如“TMD”等字符,还可以对发送图片大小进行控制。使用Mediator模式来设计。
类图:

代码:

Public abstract class Room{
Public void bemember(Member name);
Public void sendtext(string f,string t,stringcontext);
Public void sendimage(sring f,string t,string image);
}

Public abstract chat extends Room{
  Public bemember(string name){ this.name=name;}
  Public sendtext(string f,string t,string context){
     If(context.equals(“TMD”))
       System.out.println(“wrong”);
Else system.out.println(“send success”);
}
Public sendimg(string f,string t,string image){
     If(image is too big)
       System.out.println(“wrong”);
Else system.out.println(“send success”);
}
}
Public class Member{
Private String name;
Public String member(string name){
This.name=name
}
Public string sendtext(string s,string t,string context){
   Room.sendtext(s,t,context);
}
Public string sendimage(string s,string t,string image){
   Room.sendimage(s,t,image);
}
}
Public class DiamonMember extend Member{
Public commonmembername(string name)
{ super(name);}
Public sendtext(s,t,context){
Super.sendtext(s,t,context);
}
Public sendimage(s,t,context){
Super.sendimage(s,t,image);
}
}
Public class CommonMember extend Member{
Public diomandmembername(string name)
{ super(name);}
Public sendtext(s,t,context){
Super.sendtext(s,t,context);
}
}

4、要求:设计一个网上书店,对系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有15元的折扣。使用Strategy模式来设计。
类图:

代码:

Public interface BookStore{
  Double caculate();
}

Public class ComputerBookStore implements BookStore{
  Public double sale(double booksPrice)
{
Return bookPrice * 0.9;
}
}
Public class LanguageBookStore implements BookStore{
  Public double sale(double booksPrice)
{
Return bookPrice-2;
}
}
Public class NovelBookStore implements BookStore{
  Public double sale(double booksPrice)
{
Return bookPrice-bookPrice/100*15;
}
}
Public class Price{
Private BookStore bookleibie;
Public leibie(BookStore bookleibie)
{
This.bookleibie=bookleibie;
}
Public double sale(double bookpay){
Return this.bookleibie.sale(booksPrice);
}
Public class client()
{
Price pri=new Price();
If(Pri.leibie==com)
   ComputerBookStore(pri.price);
If(Pri.leibie==lan)
   LanguageBookStore(pri.price);
If(Pri.leibie==nov)
   NovelBookStore(pri.price);

}


posted @ 2017-11-26 19:16  小怪兽1  阅读(272)  评论(2编辑  收藏  举报