设计模式NO.3

设计模式NO.3

本次博客内容为第三次设计模式的练习。根据老师的要求完成下列题目:

题目1

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

代码实现如下:
a.Iterator接口


public interface Iterator {
	public boolean hasNext();
	public boolean hasFront();
	public String next();
	public String front();
	public void setPositionOrder();
	public void setPositionReverse();
	
}

b.具体Iterator类


public class MyIterator implements Iterator{

	private Aggregate m_agg;
	private int current;
	private int size;
	
	public MyIterator(Aggregate m_agg)
	{
		current=0;
		size = m_agg.getLength();
		this.m_agg=m_agg;
	}
	@Override
	public boolean hasNext() {
		if(current<size)
			return true;
			
		else
			return false;
	}

	@Override
	public boolean hasFront() {
		if(current>=0)
			return true;
		else
			return false;
	}
	@Override
	public String next() {
		
		return m_agg.getName(current++);
	}
	@Override
	public String front() {
		
		return m_agg.getName(current--);
	}
	
	@Override
	public void setPositionOrder() {
		current = 0;
		
	}
	@Override
	public void setPositionReverse() {
		current = size-1;
		
	}

}

c.Aggregate接口


public interface Aggregate {
	public Iterator CreateIterator();
	public int getLength();
	public String getName(int position);
	public void addName(String name);

}

d.具体物品类


public class GoodList implements Aggregate{
	private String names[];
	private int num;

	public GoodList(int MaxNum)
	{
		num=0;
		this.names = new String[MaxNum];
	}
	public int getLength()
	{
		return num;
	}
	public String getName(int position)
	{
			return names[position];
	}
	public void addName(String name)
	{
		names[num++]=name;
	}
	
	@Override
	public Iterator CreateIterator() {
		return new MyIterator(this);
	}

}

e.测试代码


public class client {
	public static void main(String[] args) {
		Aggregate agg = new GoodList(100);
		agg.addName("洗手液");
		agg.addName("洗衣液");
		agg.addName("牙膏");
		agg.addName("洗发露");
		Iterator it = agg.CreateIterator();
		
		Orderview(it);
		ReverseView(it);
	}
	private static void Orderview(Iterator it)
	{
		it.setPositionOrder();
		while(it.hasNext())
			System.out.println(it.next());
	}
	private static void ReverseView(Iterator it)
	{
		it.setPositionReverse();
		while(it.hasFront())
			System.out.println(it.front());
	}

}

题目2

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

代码实现如下:
a.抽象命令类


public abstract class Command {
	protected BoardScreen bs;
	
	public Command(BoardScreen bs)
	{
		this.bs = bs;
	}
	public abstract void execute();

}

b.CreateCommand类


public class CreateCommand extends Command{

	public CreateCommand(BoardScreen bs) {
		super(bs);
	
	}

	@Override
	public void execute() {
		bs.create();
		
	}

}

c.OpenCommand类


public class OpenCommand extends Command{

	public OpenCommand(BoardScreen bs) {
		super(bs);
	}

	@Override
	public void execute() {
		bs.open();
	}

}

d.EditCommand类

public class EditCommand extends Command{

	public EditCommand(BoardScreen bs) {
		super(bs);
	}

	@Override
	public void execute() {
		bs.edit();
		
	}

}

e.BoardScreen类


public class BoardScreen {
	
	public void open()
	{
		System.out.println("BoardScreen is opening!");
	}
	public void create()
	{
		System.out.println("BoardScreen is creating!");
	}
	
	public void edit()
	{
		System.out.println("BoardScreen is editing!");
	}
}

f.Menu类

public class Menu {
	
	private ArrayList<MenuItem> items = new ArrayList<MenuItem>();
	
	public void addItem(MenuItem item)
	{
		items.add(item);
	}
	
	public void clickItem(int index)
	{
		items.get(index).click();
	}

}

g.MenuItem类


public class MenuItem {
	private Command com;
	
	public MenuItem(Command com)
	{
		this.com=com;
	}
	public void click()
	{
		com.execute();
	}

}

h.测试类


public class client {
	public static void main(String[] args) {
		BoardScreen bs = new BoardScreen();
		Menu menus = new Menu();
		
		Command command = new CreateCommand(bs);
		menus.addItem(new MenuItem(command));
		
		command = new OpenCommand(bs);
		menus.addItem(new MenuItem(command));
		
		command = new EditCommand(bs);
		menus.addItem(new MenuItem(command));
		
		menus.clickItem(0);//BoardScreen is creating!
		menus.clickItem(1);//BoardScreen is opening!
		menus.clickItem(2);//BoardScreen is editing!
	}
	
}

题目3

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

代码设计如下:
a.抽象Member类


public abstract class Member {
	protected ChatRoom cr;
	protected String name;
	
	public Member(String name)
	{
		this.name = name;
	}
	
	public abstract void setChatRoom(ChatRoom cr);
	public abstract void sendPic(String pic);
	public abstract void sendText(String text);
}

b.抽象聊天室类


public abstract class ChatRoom {
	public abstract void sendPic(String pic);
	public abstract void sendText(String text);

}

c.CommonMember类


public class CommonMember extends Member{

	public CommonMember(String name) {
		super(name);
	}

	@Override
	public void setChatRoom(ChatRoom cr) {
		this.cr=cr;
		
	}

	@Override
	public void sendPic(String pic) {
		System.out.println("only DiamonMember can send picture!" );
		
	}

	@Override
	public void sendText(String text) {
		System.out.print("CommonMember ["+name+"] says: ");
		cr.sendText(text);
		
	}

}

d.DiamondMember类


public class DiamondMember extends Member{

	public DiamondMember(String name) {
		super(name);
	}

	@Override
	public void setChatRoom(ChatRoom cr) {
		this.cr=cr;
		
	}

	@Override
	public void sendPic(String pic) {
		System.out.print("DiamondMember ["+name+"] send a picture: ");
		cr.sendPic(pic);
		
	}

	@Override
	public void sendText(String text) {
		System.out.print("DiamondMember ["+name+"] says: ");
		cr.sendText(text);
	}
	
	
}

e.RealChatRoom具体类


public class RealChatRoom extends ChatRoom{

	@Override
	public void sendPic(String pic) {
		if(PicCheck(pic))
		{
			System.out.println(pic);
		}
		else
			System.out.println("the size of picture is too large");
		
	}

	@Override
	public void sendText(String text) {
		System.out.println(TextFilter(text));
		
	}
	
	private boolean PicCheck(String Pic)
	{
		if(Pic.length()>9)
			return false;
		else
			return true;
	}
	
	private String TextFilter(String text)
	{
		return text.replaceAll("[Tt][Mm][Dd]", "***");
	}


}

f.测试类

public class client {
	public static void main(String[] args) {
		ChatRoom cr = new RealChatRoom();
		
		Member m1 = new CommonMember("Jack");
		Member m2 = new DiamondMember("Will");
		
		m1.setChatRoom(cr);
		m2.setChatRoom(cr);
		
		m1.sendText("tmd,NTMD,blablabla....");//CommonMember [Jack] says: ***,N***,blablabla....
		m1.sendPic("gogogogogogogogog");//only DiamonMember can send picture!
		
		m2.sendText("tmd,NTMD,blablabla....");//DiamondMember [Will] says: ***,N***,blablabla....
		m2.sendPic("good");//DiamondMember [Will] send a picture: good
		m2.sendPic("goodgoodgoodgoodgoodgood");//DiamondMember [Will] send a picture: the size of picture is too large
	}

}

题目4

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

代码设计如下:
a.抽象Discount类


public abstract class Discount {

	public abstract double discount(double price);
}

b.ComputerDiscount类

public class ComputerDiscount extends Discount{

	@Override
	public double discount(double price) {
		return price*0.9;
	}

}

c.LanguageDiscount类


public class LanguageDiscount extends Discount{

	@Override
	public double discount(double price) {
		return price-2;
	}

}

d.NovelDiscount类


public class NovelDiscount extends Discount{

	@Override
	public double discount(double price) {
		return price-(price/100)*15;
	}
}

e.Book类

public class Book {
	private double price;
	private Discount dis;
	public Book(Discount dis)
	{
		this.dis=dis;
	}
	
	public void setDiscount(Discount dis)
	{
		this.dis=dis;
	}
	public void setPrice(double price)
	{
		this.price = price;
	}
	public double getPrice()
	{
		return dis.discount(price);
	}
}

f.测试类

public class client {
	public static void main(String[] args) {
		Book computer = new Book(new ComputerDiscount());
		computer.setPrice(100.0);
		
		Book language = new Book(new LanguageDiscount());
		language.setPrice(100.0);
		
		Book novel = new Book(new NovelDiscount());
		novel.setPrice(100.0);
		
		System.out.println(computer.getPrice());//90.0
		System.out.println(language.getPrice());//98.0
		System.out.println(novel.getPrice());//85.0
		
		
	}

}
posted @ 2017-11-24 15:14  会煮面  阅读(221)  评论(1编辑  收藏  举报