zoukankan      html  css  js  c++  java
  • 设计模式第三次作业

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

    public interface Iterator {
        public abstract boolean next();
        public abstract boolean previous();
    }
    
    public class MyIterator implements Iterator {
        private MyGoodsStore mygoodsstore;
        private int index;
        public MyIterator(MyGoodsStore mygoodsstore) 
        {
            this.mygoodsstore = mygoodsstore;
            this.index = 0;
        }
        @Override
        public boolean next() 
        {
        }
        @Override
        public boolean previous() 
        {
        }
    }
    
    public interface GoodsStore {
        public abstract Iterator iterator();
    }
    
    
    public class MyGoodsStore implements GoodsStore {
    
        private Goods[] goods;
    
        public MyGoods(int maxSize) {
            this.goods = new Goods[maxSize];
    
        }
    
        @Override
        public Iterator iterator() {
            return new MyIterator(this);
        }
    }
    

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

    public class Menu 
    {
        private List<MenuItem> myMenuItemList = new ArrayList<>();
    
        public void addMenuItem(MenuItem menuItem) 
        {
        }
    
        public void clickMenuItem(int index) 
        {
            menuItemList.get(index).click();
        }
    }
    
    
    public class MenuItem 
    {
        private Command command;
    
        public MenuItem(Command command) 
        {
            this.command = command;
        }
    
        public void setCommand(Command command) 
        {
            this.command = command;
        }
    
        public void click() 
        {
            command.execute();
        }
    }
    
    
    public abstract class Command 
    {
        public abstract void execute();
    }
    
    public class BoardScreen
    {
    	public bool open()
    	{
    
    	}
    
    	public bool create()
    	{
    
    	}
    
    	public bool edit()
    	{
    		
    	}
    }
    
    
    public class OpenCommand extends Command 
    {
    
        private BoardScreen boardScreen;
    
        public OpenCommand(BoardScreen boardScreen) 
        {
            this.boardScreen = boardScreen;
        }
    
        @Override
        public void execute() 
        {
            boardScreen.open();
        }
    }
    
    public class CreateCommand extends Command 
    {
    
        private BoardScreen boardScreen;
    
        public CreateCommand(BoardScreen boardScreen) 
        {
            this.boardScreen = boardScreen;
        }
    
        @Override
        public void execute() 
        {
            boardScreen.create();
        }
    }
    
    
    public class EditCommand extends Command 
    {
    
        private BoardScreen boardScreen;
    
        public EditCommand(BoardScreen boardScreen) 
        {
            this.boardScreen = boardScreen;
        }
    
        @Override
        public void execute() {
            boardScreen.edit();
        }
    }
    
    

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

    public abstract class Member 
    {
        private String member_name;
        private ChatRoom chat_room;
    
        public Member(String member_name) 
        {
            this.member_name = member_name;
        }
    
        public void setRoom(ChatRoom chat_room) 
        {
            this.chat_room = chat_room;
        }
    
        public String getMemberName() 
        {
            return member_name;
        }
    
        public boolean sendText(Member receiver, String text)
        {
        }
    
        public boolean sendImage(Member receiver, String image)
        {
        }
    }
    
    
    public class CommonMember extends Member
    {
        public CommonMember(String member_name) 
        {
            super(member_name);
        }
    
        public boolean sendImage(Member receiver, String image) 
        {
        }
    }
    
    
    public class DiamondMember extends Member 
    {
        public DiamondMember(String member_name) 
        {
            super(member_name);
        }
    }
    
    
    public class ChatRoom 
    {
        public boolean register(Member member)
        {
            member.setRoom(this);
        }
    
        public boolean sendText(Member sender, Member receiver, String text)
        {
        }
        public boolean sendImage(Member sender, Member receiver, String image)
        {
        }
    }
    
    
    public class ConcreteChatRoom extends ChatRoom
    {
        protected String textFilter(String text)
        {
        }
    
        protected boolean imageCheck(String image)
        {
        }
    
    
        public boolean sendText(Member sender, Member receiver, String text) 
        {
            super.sendText(sender, receiver, textFilter(text));
        }
    
        public boolean sendImage(Member sender, Member receiver, String image) 
        {
            if(imageCheck(image))
            {
                super.sendImage(sender, receiver, image);
            }
        }
    
        
    }
    
    
    

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

    public abstract class Discount 
    {
        public double discount(double price);
    }
    
    public class ComputerBookDct extends Discount
    {
        public double discount(double price) 
        {
            return 0.9*price;
        }
    }
    
    
    public class LanguageBookDct extends Discount
    {
        public double discount(double price) 
        {
            if(price > 2)
                return price-2;
            else return price;
        }
    
    }
    
    public class NovelBookDct extends Discount
    {
        public double discount(double price) 
        {
            if(price >= 100) 
                return price-((int)price/100)*15;
            else
                return price;
        }
    
    }
    
    
    
    public class BookShop 
    {
        private ComputerBookDct computer_book_dct;
        private LanguageBookDct language_book_dct;
        private NovelBookDct novel_book_dct;
    
        public void setComputerBookDct(ComputerBookDct computer_book_dct) 
        {
            this.computer_book_dct = computer_book_dct;
        }
    
        public void setLanguageBookDct(LanguageBookDct language_book_dct) 
        {
            this.language_book_dct = language_book_dct;
        }
    
        public void setNovelBookDct(NovelBookDct novel_book_dct) 
        {
            this.novel_book_dct = novel_book_dct;
        }
    
        public void showBookPrice(Book book)
        {
            double book_price = book.getPrice();
            if(book instanceof ComputerBook)
            {
                book_price = computer_book_dct.discount(book_price);
            }
            else if(book instanceof LanguageBook)
            {
                book_price = language_book_dct.discount(book_price);
            }
            else if(book instanceof NovelBook)
            {
                book_price = novel_book_dct.discount(book_price);
            }
    
            System.out.println(book.getClass().getSimpleName()+"  原价格:"+book.getPrice()+"  现价格:"+book_price );
        }
    }
    
    
    
    public abstract class Book 
    {
        private double book_price;
    
        public double getBookPrice() 
        {
            return book_price;
        }
    
        public void setBookPrice(double book_price) 
        {
            this.book_price = book_price;
        }
    }
    
    
    public class ComputerBook extends Book 
    {
    }
    
    
    public class LanguageBook extends Book 
    {
    }
    
    
    public class NovelBook extends Book 
    {
    }
    
    
  • 相关阅读:
    Ubuntu上安装Redis
    Unity Shader中将指定颜色过滤成透明
    用Python发送邮件
    Flask搭建简单的服务器
    SQLServer 中All、Any和Some用法与区别
    Linux探秘之用户态与内核态
    MTDDL 美团点评分布式数据访问层中间件
    基础数据结构 例:栈、队列、链表、数据、字典、树、等
    二叉树、红黑树、B&B+树数据结构
    CPU,GPU,高速缓存cache,内存RAM,虚拟内存VM,磁盘ROM,磁盘缓存之间的关系
  • 原文地址:https://www.cnblogs.com/maskerk/p/7912615.html
Copyright © 2011-2022 走看看