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

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

    类图如下所示

    关键类代码及注释如下

    
    abstract class AbstractProductVector {  
        //抽象商品向量
        private String[] productName;  
          
        public AbstractProductList(String[] productName) {  
            this.productsName = productsName;  
        }  
          
        public String[] getProductsName(){  
            return this.productsName;  
        }  
        public abstract AbstractIterator getIterator();  
    }  
    
    public interface AbstractIterator {  
        //迭代器接口
        public void next();  
        public boolean isLast();  
        public void previous();  
        public boolean isFirst();  
        public String getNextItem();  
        public String getPreviousItem(); 
    	public String getCurrentItem(); 
    }  
    
    class MyIterator implements AbstractIterator{  
        //商品类迭代器
        private String[] productName;  
    	private int index;
          
        public MyIterator(AbstractProductVectoe vect) {  
            // 用商品类来初始化构造函数 
            productsName = vect.getProductName(); 
    		index = 0;  
        }  
           
        public void next() {  
            // TODO Auto-generated method stub    
                index=(index+1)%productName.length; 
        }  
      
        public boolean isLast() {  
            // TODO Auto-generated method stub  
            return (index == productName.length-1);  
        }  
       
        public void previous() {  
            // TODO Auto-generated method stub  
            index=(index-1)%productName.length; 
        }  
      
        public boolean isFirst() {  
            // TODO Auto-generated method stub  
            return index==0;  
        }  
      
         
        public String getNextItem() {   
            return productName[(index+1)%productName.length];  
        }  
       
        public String getPreviousItem() {  
            // TODO Auto-generated method stub  
           public String getCurrentItem() {   
            return productName[index];  
        } 
        
    }  
    
    class ProductVector extends AbstractProductVector{  
        //商品类
        public ProductVector(String[] productName) {  
            super(productName);  
        }  
      
        public AbstractIterator getIterator() {  
            // TODO Auto-generated method stub  
            return new MyIterator(this);  
        }  
    }  
    
    

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

    类图如下所示

    关键代码及注释如下

    public abstract class Command {
    	//抽象命令类
    	protected BoardScreen bs = new BoardScreen();
        public abstract void execute();
    }
    
    public class createCommand extends Command {
        //create命令
        public void execute() {
            bs.create();
        }
    }
    
    public class openCommand extends Command {
        //open
        public void execute() {
            bs.open();
        }
    }
    
    public class editCommand extends Command {
        //edit命令
        public void execute() {
            bs.edit();
    	}
    }
    
    public class MenuItem {
    	//菜单项,充当调用者的角色
        private Command command;
        public MenuItem(Command c){
            this.setCommand(c);
        }
        public void setCommand(Command c){
                command = c;
        }
        public void click(){
            command.execute();
        }
    }
    
    public class BoardScreen {
    	//界面类,充当接收者的角色
        public void open(){
            System.out.println("打开BoardScreen");
        }
        public void create(){
            System.out.println("新建BoardScreen");
        }
        public void edit(){
            System.out.println("编辑BoardScreen");
        }
    }
    
    public class Menu {
    	//客户端
        private List<MenuItem> MenuItems = new ArrayList<MenuItem>();
        public void addMenuItem(MenuItem mi){
            MenuItems.add(mi);
        } 
        public void click(){
            //打开菜单项
        }
    }
     
    
    

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

    类图如下所示

    关键类代码及注释如下

    public abstract class ChatRoom
    {
    	//抽象聊天室类
        public abstract void register(Member member);
        public abstract void sendText(String from,String to,String message);
    	public abstract void sendImg(String from,String to,String message);
    }
    
    public class ChatGroup extends ChatRoom
    {
    	public void register(Member mem)
    	{
    		//注册
    	}
    	public void sendText(String from, String to ,String message)
    	{
    		//发送消息
    	}
    	public void sendImg(String from, String to, String img)
    	{
    		//发送图片
    	}
    }
    
    
    public abstract class Member 
    { 
    	//抽象成员类
    	protected String name;  
    	protected ChatRoom chatroom; 
    	public Member(String name) 
    	{ 
    	this.name=name; 
    	}  
    	public String getName() 
    	{  
    	return name; 
    	} 
    	public void setName(String name) 
    	{ 
    	this.name=name; 
    	} 
    } 
    
    public class DiamondMember extends Member
    {
    	//钻石会员
    	public DiamondMember(String name)
    	{
    		super(name);
    	}
    	
    	public void sendText(String to,String message)
    	{
    		super.room.sendText(this.name,to,message);
    	}
    	
    	public void sendImg(String to,String message)
    	{
    		super.room.sendImg(this.name,to,img);
    	}
    }
    
    public class CommonMember extends Member
    {
    	//普通会员
    	public CommonMember(String name)
    	{
    		super(name);
    	}
    	
    	public void sendText(String to,String message)
    	{
    		super.room.sendText(this.name,to,message);
    	}
    	
    	public void sendImg(String to,String message)
    	{
    		System.out.printIn("普通会员不能发送图片");
    	}
    }
    
    

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

    类图如下所示

    关键类代码及注释如下

    public abstract class DiscountStrategy
    {
    	//抽象折扣类
    	public abstract double count(int amount,double price);
    }
    
    public class ComputerDiscountStrategy extends DiscountStrategy
    {
    	//计算机类折扣策略
    	public double count(int amount,double price)
    	{
    		return amount*price*0.9;
    	}
    }
    
    public class LanguageDiscountStrategy extends DiscountStrategy
    {
    	//语言类折扣策略
    	public double count(int amount,double price)
    	{
    		return amount*(price-2);
    	}
    }
    
    public class NovelDiscountStrategy extends DiscountStrategy
    {
    	//小说折扣策略
    	public double count(int amount,double price)
    	{
    		return amount*price-(amount*price)/100*15;
    	}
    }
    public class NulDiscountStrategy extends DiscountStrategy
    {
    	//不打折
    	public double count(int amount,double price)
    	{
    		return amount*price;
    	}
    }
    public class BookOrder
    {
    	//购买清单
    	private int bookID;
    	private double price;
    	private String type;
    	private int amount;
    	private DiscountStrategy strategy;
    	
    	public void setDiscountStrategy()
    	{
    		if(this.type.equals("computer"))
    		{
    			this.strategy=new ComputerDiscountStrategy();
    		}
    		else if(this.type.equals("language"))
    		{
    			this.strategy=new LanguageDiscountStrategy();
    		}else if(this.type.equals("novel"))
    		{
    			this.strategy=new NovelDiscountStrategy();
    		}else
    		{
    			this.strategy=new NullDiscountStrategy();
    		}
    	}
    	
    	public double getOrderMoney()
    	{
    		double bill=this.strategy.count(this.amount,this.price);
    	}
    }
    public class BookCart
    {        //购物车
    	private ArrayList<BookOrder> list=new ArrayList<BookOrder> ();
    	public void addBookOrder(BookOrder order)
    	{
    		list.add(order);
    	}
    	public void deleteBookOrder(BookOrder order)
    	{
    		list.remove(order);
    	}
    	public double payBill()
    	{
    		double totalMoney=0;
    		for(int i=0;i<list.size;i++)
    		{
    			totalMoney+=list.get(i).getOrderMoney();
    		}
    	}
    }
    
  • 相关阅读:
    iptables和DNS
    centos6.5下载
    linux 系统版本信息
    如何挂载
    Linux网络命令
    端口网络等
    linux安装tomcat
    ip设置
    最全DOS的CMD命令,程序员必会
    c语言文件分割与合并
  • 原文地址:https://www.cnblogs.com/liminghuang/p/7898020.html
Copyright © 2011-2022 走看看