标签:ast ret 主菜单 type imp 容器 tor get 有一个
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);
}
}
标签:ast ret 主菜单 type imp 容器 tor get 有一个
原文地址:http://www.cnblogs.com/xiezhe1204/p/7900140.html