設計模式第三次作業
設計模式第三次作業
1.要求:某商品管理系統的商品名稱存儲在一個字符串數組中,現需要自定義一個雙向叠代器(MyIterator)實現對該商品名稱數組的雙向(向前和向後)遍歷。使用Iterator模式來設計。
抽象叠代器
public interface AbstractIterator {
public String next();
public String last();
public int hasNext();//擁有查看是否到邊界的功能,包括頭和尾
public String currentIterator();
}
抽象容器
public interface AbstractStringArray{ public abstract AbstractIterator createIterator(); }
商品容器
public class StringArray implements AbstractStringArray{ private String[] goods= { "milk","cookie","wine","noodles","rice" }; public int getLength() { return goods.length; } public String getItem(int positon) { return goods[position]; } public AbstractIterator createIterator() { return new MyIterator(); } }
雙向叠代器
public class MyIterator implements AbstractIterator{ public MyIterator(StringArray stringArray) { this.stringArray=stringArray; } private StringArray stringArray; private int index=0; public int hasNext{ if(index>=stringArray.getLength()&&index<=0) { return 0; } else return 1; } public String next() { if(hasNext==0) { return stringArray.getItem(index); index++; } } public String last() { if(hasNext==0) { return stringArray.getItem(index); index--; } } public String currentItem() { return stringArray.getItem(index); } }
客戶類
public class Client{
StringArray stringArray;
AbstractIterator iterator=stringArray.createIterator();
public void nextRead() {
for(int i=0;i<iterator.hasNext();i++) {
System.out.println(iterator.currentIterator());
iterator.next();
}
}
public void lastRead() {
for(int i=stringArray.getLength();i<iterator.hasNext();i--) {
System.out.println(iterator.currentIterator());
iterator.last();
}
}
}
2.某公司欲開發一個基於Windows平臺的公告板系統,系統提供主菜單(Menu)其中有菜單項(MenuItem)。通過Menu類的addMenuItem()方法增加菜單項。菜單項的打開方法是click(),每個菜單項包含一個抽象命令類,具體命令類包括OpenCommand()、CreateCommand()、EditCommand()等,命令類具有一個execute()方法,用於調用公告板系統界面類(BoardScreen())的open()、create()、edit())等方法。使用Command模式來設計。
抽象命令類
public abstract class Command {
public void execute() {};
}
接收者boardscreen類
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 OpenCommand extends Command{
private BoardScreen boardScreen=null;
public OpenCommand(BoardScreen boardScreen) {
this.boardScreen=boardScreen;
}
public void execute() {
boardScreen.open();
}
}
public class CreateCommand extends Command{
private BoardScreen boardScreen=null;
public CreateCommand(BoardScreen boardScreen) {
this.boardScreen=boardScreen;
}
public void execute() {
boardScreen.create();
}
}
public class EditCommand extends Command{
private BoardScreen boardScreen=null;
public EditCommand(BoardScreen boardScreen) {
this.boardScreen=boardScreen;
}
public void execute() {
boardScreen.edit();
}
}
請求者類
public class MenuItem{
private Command openCommand=null;
private Command createCommand=null;
private Command editCommand=null;
MenuItem(Command openCommand,Command createCommmand,Command editCommand){
this.openCommand=openCommand;
this.createCommand=createCommmand;
this.editCommand=editCommand;
}
public void openC(){
openCommand.execute();
}
public void createC(){
createCommand.execute();
}
public void editC(){
editCommand.execute();
}
}
菜單類
public class Menu{
private List<MenuItem> menuList=new ArrayList<>();
public void addMenuItem(MenuItem MenuItem) {
menuList.add(MenuItem);
}
public MenuItem getMenuItem(int index) {
return menuList.get(index);
}
}
客戶類(測試類)
public class Client{
public static void main() {
//接受者
BoardScreen boardScreen=new BoardScreen();
Menu menu=new Menu();
//命令對象
OpenCommand open=new OpenCommand(boardScreen);
CreateCommand create=new CreateCommand(boardScreen);
EditCommand edit=new EditCommand(boardScreen);
//請求者
MenuItem menuItem=new MenuItem(open,create,edit);
menu.addMenuItem(menuItem);
//測試
menuItem.createC();
menuItem.editC();
menuItem.openC();
}
}
3.某論壇系統欲增加一個虛擬聊天室,允許論壇會員通過該聊天室進行信息交流,普通會員(CommonMember)可以給其他會員發送文本信息,鉆石會員(DiamondMember)可以給其他會員發送文本和圖片信息。該聊天室可以對不雅字符進行過濾,如“TMD”等字符,還可以對發送圖片大小進行控制。使用Mediator模式來設計。
抽象聊天室
public abstract class AbstractLobby {
public abstract void sendImage();
public abstract void sendLetter();
}
抽象成員
public class Member{
private AbstractLobby abstractLobby;
private String name;
public String getName() {
return name;
}
Member(AbstractLobby abstractLobby,String name){
this.abstractLobby=abstractLobby;
this.name=name;
}
}
鉆石會員
public class DiamondMember extends Member{
DiamondMember(AbstractLobby abstractLobby,String name){
super(abstractLobby,name);
}
public void sendImage(String image) {
System.out.println("DiamondMember "+this.getName()+" send image "+image);
}
public void sendLetter(String letter) {
System.out.println("DiamondMember"+this.getName()+" send message "+letter);
}
}
普通會員
public class CommonMember extends Member{
CommonMember(AbstractLobby abstractLobby,String name){
super(abstractLobby,name);
}
public void sendLetter(String letter) {
System.out.println("CommonMember"+this.getName()+" send message "+letter);
}
}
聊天室
public class Lobby extends AbstractLobby{
DiamondMember dm;
CommonMember cm;
private int size=5;
public void setSize(int size) {
this.size=size;
}
//判斷規範
public String checkLetter(String letter) {
letter.replace("TMD","!@#");
letter.replace("fuck", "!@#");
return letter;
}
//判斷大小
public boolean checkImage(String image) {
if(image.length()>size)
return false;
else return true;
}
public void setDM(DiamondMember dm) {
this.dm=dm;
}
public void setCM(CommonMember cm) {
this.cm=cm;
}
//判斷圖片是否符合限制並發送
public void sendImage(String image) {
if(checkImage(image))
dm.sendImage(image);
else
System.out.println("send failed,Too large to send");
}
//判斷是否出現敏感詞並發送
public void sendLetter(String letter,Member member) {
if(member==dm) {
dm.sendLetter(checkLetter(letter));
}
else
cm.sendLetter(checkLetter(letter));
}
}
客戶類
public class Client{
public static void main(String[] args) {
Lobby lobby=new Lobby();
DiamondMember diamondMember=new DiamondMember(lobby,"ash");
CommonMember commonMember=new CommonMember(lobby,"jackle");
//添加入聊天室
lobby.setDM(diamondMember);
lobby.setCM(commonMember);
xn--wcs23c1yd9t9a76olnr
lobby.setSize(10);
//測試
diamondMember.sendImage("imageSize");
diamondMember.sendLetter("what the fuck!!!");
commonMember.sendLetter("what the hell!!!");
}
}
4.設計一個網上書店,對系統中所有的計算機類圖書(ComputerBook)每本都有10%的折扣,語言類圖書(LanguageBook)每本都有2元的折扣,小說類圖書(NovelBook)每100元有15元的折扣。使用Strategy模式來設計。
折扣策略類
public interface OffStrategy {
public double paymentCal(double bookPrice);
}
計算機折扣類
public class ComputerBook implements OffStrategy{
public double paymentCal(double bookPrice) {
return bookPrice*0.9;
}
}
語言書折扣類
public class LanguageBook implements OffStrategy{
public double paymentCal(double bookPrice) {
return bookPrice-2.0;
}
}
小說折扣類
public class NovelBook implements OffStrategy{
public double paymentCal(double bookPrice) {
return bookPrice-15*((int)bookPrice/100);
}
}
價格類(context)
public class Price{
private OffStrategy offStrategy;
public Price(OffStrategy offStrategy) {
this.offStrategy=offStrategy;
}
public void setBookType(OffStrategy offStrategy) {
this.offStrategy=offStrategy;
}
public double bill(double bookPrice) {
return this.offStrategy.paymentCal(bookPrice);
}
}
客戶類(測試類)
public class Client{
public static void main(String[] args) {
OffStrategy computer=new ComputerBook();
OffStrategy language=new LanguageBook();
OffStrategy novel=new NovelBook();
double bookBill=0;
Price price=new Price(computer);
double bookPrice=price.bill(240);
bookBill=bookPrice+bookBill;
System.out.println("bookprice is "+bookPrice);
price.setBookType(language);
bookPrice=price.bill(400);
bookBill=bookPrice+bookBill;
System.out.println("bookprice is "+bookPrice);
price.setBookType(novel);
bookPrice=price.bill(310);
bookBill=bookPrice+bookBill;
System.out.println("bookprice is "+bookPrice);
System.out.println("total price is "+bookBill);
}
}
設計模式第三次作業