博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式第三次作业
阅读量:6278 次
发布时间:2019-06-22

本文共 9862 字,大约阅读时间需要 32 分钟。

设计模式第三次作业

GitHub地址:https://github.com/zlxzlxzlx/DP3

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

    类图:1249529-20171124131425375-548076324.png

关键类java源码及注释:

public interface abstructIterator {        public void next();          public boolean isLast();          public void previous();          public boolean isFirst();          public String getNext();          public String getPrevious();  }
public interface abstructProduct {   abstructIterator createIterator();}
public class Product implements abstructProduct{    private  Object[] obj={"苹果","梨","橙子","香蕉"};    @Override    public abstructIterator createIterator() {        return new MyIterator();    }    private class MyIterator implements abstructIterator{         private int index1;           private int index2;                   public MyIterator() {                  index1 = 0;                  index2 = obj.length - 1;              }                   @Override        public void next() {              if(index1
-1) { index2--; } } @Override public boolean isFirst() { return index2==-1; } @Override public String getNext() { return (String) obj[index1]; } @Override public String getPrevious() { return (String) obj[index2]; } } }
public class client {    public static void main(String[] args) {    abstructIterator iterator;    abstructProduct product =new Product();    iterator=product.createIterator();    while (!iterator.isLast()) {          System.out.print(iterator.getNext());          System.out.print(" ");         iterator.next();         }      System.out.println();     while (!iterator.isFirst()) {           System.out.print(iterator.getPrevious());           System.out.print(" ");          iterator.previous();       }      }}

输出结果:

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

    类图:1249529-20171124131434468-1002956219.png

关键类java源码及注释:

public interface abstructCommand {    public void execute();}
public class CreateCommand implements abstructCommand{    private  BoardScreen bs;       public CreateCommand( BoardScreen bs){        this.bs=bs;    }    @Override    public void execute() {        bs.create();    }}
public class EditCommand implements abstructCommand{    private  BoardScreen bs;    public EditCommand ( BoardScreen bs){        this.bs=bs;    }    @Override    public void execute() {        bs.edit();    }}
public class OpenCommand implements abstructCommand{    private  BoardScreen bs;    public OpenCommand ( BoardScreen bs){        this.bs=bs;    }    @Override    public void execute() {        bs.open();    }}
public class MenuItem {     private abstructCommand command;     public MenuItem(abstructCommand command){        this.command=command;      }     public void click(){         this.command.execute();     }}
import java.util.ArrayList;public class Menu {    private ArrayList menuItemsList = new ArrayList();    public void addMenuItem(MenuItem item){        this.menuItemsList.add(item);    }         public void clickMenuItem(int index) {         menuItemsList.get(index).click();        }}
public class BoardScreen {     public void open(){        System.out.println("open被调用");      }     public void create(){        System.out.println("create被调用");       }     public void edit(){        System.out.println("edit被调用");       }}
public class client {    public static void main(String[] args) {         Menu menu=new Menu();         BoardScreen bs = new BoardScreen();     abstructCommand  oc=new OpenCommand(bs );     MenuItem item1=new MenuItem(oc);     menu.addMenuItem(item1);     abstructCommand  cc=new CreateCommand(bs );         MenuItem item2=new MenuItem(cc);     menu.addMenuItem(item2);     abstructCommand  ec=new EditCommand(bs );         MenuItem item3=new MenuItem(ec);     menu.addMenuItem(item3);           menu.clickMenuItem(0);     menu.clickMenuItem(1);     menu.clickMenuItem(2);    }}

输出结果:

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

    类图:1249529-20171124131448343-1419685008.png

关键类java源码及注释:

public abstract class abstructChatRoom {    public abstract  void addRoom(Member member);  public abstract  void sendText(String form,String to,String text);  public abstract void sendImage(String form,String to,String image);}
import java.util.HashMap;import java.util.Map;public class ChatRoom extends abstructChatRoom{    Map
map=new HashMap
(); @Override public void addRoom(Member member) { map.put(member.getName(), member); member.setAbstructChatRoom(this); } @Override public void sendText(String from, String to, String text) { Member memberToText=map.get(to); memberToText.reciveText(from, text); } @Override public void sendImage(String from, String to, String image) { Member memberToImage=map.get(to); memberToImage.reciveImage(from, image); }}
public abstract class Member {    protected String name;    protected abstructChatRoom abstructChatRoom;   public Member(String name) {        this.name=name;    }   public String getName() {        return name;    }      public abstructChatRoom getAbstructChatRoom() {        return abstructChatRoom;    }    public void setAbstructChatRoom(abstructChatRoom abstructChatRoom) {        this.abstructChatRoom = abstructChatRoom;    }    public abstract void sendText(String to,String Text);    public abstract void sendImage(String to,String Image);    public void reciveText(String from,String Text){       System.out.println(this.name+"接收来自"+from+"的文本:"+Text);       }    public void reciveImage(String from,String Image){        System.out.println(this.name+"接收来自"+from+"的图片:"+Image);       }}
public class DiamondMember extends Member{        public DiamondMember(String name){        super(name);    }    @Override    public void sendText(String to, String Text) {        System.out.println(this.name+"向"+to+"发送的文本:"+Text);        super.abstructChatRoom.sendText(this.name, to, Text);    }    @Override    public void sendImage(String to, String Image) {        System.out.println(this.name+"向"+to+"发送的图片:"+Image);        super.abstructChatRoom.sendImage(this.name, to, Image);    }}
public class CommonMember extends Member {        public CommonMember(String name){        super(name);    }    @Override    public void sendText(String to, String Text) {        System.out.println(this.name+"向"+to+"发送的文本:"+Text);        super.abstructChatRoom.sendText(this.name, to, Text);    }    @Override    public void sendImage(String to, String Image) {         System.out.println("对不起您没有发送图片的权限");    }}
public class client {    public static void main(String[] args) {        abstructChatRoom chatRoom=new ChatRoom();        Member member1=new CommonMember("zlx");        chatRoom.addRoom(member1);                Member member2=new DiamondMember("lj");        chatRoom.addRoom(member2);                Member member3=new CommonMember("tf");        chatRoom.addRoom(member3);                member1.sendText("lj", "hello");        member1.sendImage("ft", "image");                member2.sendText("zlx", "fine");                member3.sendText("zlx", "thank");        member3.sendImage("lj", "image");                }}

输出结果:

zlx向lj发送的文本:hellolj接收来自zlx的文本:hello对不起您没有发送图片的权限lj向zlx发送的文本:finezlx接收来自lj的文本:finetf向zlx发送的文本:thankzlx接收来自tf的文本:thank对不起您没有发送图片的权限
  • 要求:设计一个网上书店,对系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有15元的折扣。使用Strategy模式来设计。

    类图:1249529-20171124131456078-1061232936.png

关键类java源码及注释:

public interface BookStrategy {     double calcPrice(double bookPrice); }
public class ComputerBook implements BookStrategy{    @Override    public double calcPrice(double bookPrice) {         System.out.println("ComputerBook 10% discount. ");             return bookPrice*0.9;    }}
public class LanguageBook implements BookStrategy{        @Override    public double calcPrice(double bookPrice) {         System.out.println("LanguageBook-2.0 discount.");         return bookPrice-2.0;    }}
public class NovelBook implements BookStrategy{    @Override    public double calcPrice(double bookPrice) {          System.out.println("NovelBook100.00-15.00");                   int bp = (int)bookPrice;                    return bookPrice - (double)(bp / 100 * 15);    }}
public class Price {     private BookStrategy strategy;     public void setStrategy(BookStrategy strategy){         this.strategy=strategy;     }    public double calcPrice(double bookPrice){         return this.strategy.calcPrice(bookPrice);     }}
public class client {    public static void main(String[] args) {                Price price1=new Price();        BookStrategy strategy1=new ComputerBook();        price1.setStrategy(strategy1);        double p1=123;        System.out.println("计算机类图书原价"+p1+"计算机类图书折扣后:"+price1.calcPrice(p1));                Price price2=new Price();        BookStrategy strategy2=new LanguageBook();        price2.setStrategy(strategy2);        double p2=456;        System.out.println("语言类图书原价"+p2+"语言类图书折扣后:"+price2.calcPrice(p2));                Price price3=new Price();        BookStrategy strategy3=new NovelBook();        price3.setStrategy(strategy3);        double p3=789;        System.out.println("小说类图书"+p3+"小说类图书折扣后:"+price3.calcPrice(p3));            }}

输出结果:

ComputerBook 10% discount. 计算机类图书原价123.0计算机类图书折扣后:110.7LanguageBook-2.0 discount.语言类图书原价456.0语言类图书折扣后:454.0NovelBook100.00-15.00小说类图书789.0小说类图书折扣后:684.0

转载于:https://www.cnblogs.com/zlxbky/p/7890134.html

你可能感兴趣的文章
hadoop、hbase、zookeeper集群搭建
查看>>
python中一切皆对象------类的基础(五)
查看>>
modprobe
查看>>
android中用ExpandableListView实现三级扩展列表
查看>>
%Error opening tftp://255.255.255.255/cisconet.cfg
查看>>
java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
查看>>
《从零开始学Swift》学习笔记(Day 51)——扩展构造函数
查看>>
python多线程队列安全
查看>>
[汇编语言学习笔记][第四章第一个程序的编写]
查看>>
android 打开各种文件(setDataAndType)转:
查看>>
补交:最最原始的第一次作业(当时没有选上课,所以不知道)
查看>>
Vue实例初始化的选项配置对象详解
查看>>
PLM产品技术的发展趋势 来源:e-works 作者:清软英泰 党伟升 罗先海 耿坤瑛
查看>>
vue part3.3 小案例ajax (axios) 及页面异步显示
查看>>
浅谈MVC3自定义分页
查看>>
.net中ashx文件有什么用?功能有那些,一般用在什么情况下?
查看>>
select、poll、epoll之间的区别总结[整理]【转】
查看>>
CSS基础知识(上)
查看>>
PHP中常见的面试题2(附答案)
查看>>
26.Azure备份服务器(下)
查看>>