1、要求:某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(向前和向后)遍历。使用Iterator模式来设计。
类图如下:
代码如下:
//抽象迭代器类AbstactIterator
public interface AbstactIterator {
void setPosition(int i);
void FTraversal();
void Btraversal();
String currentGoodName();
boolean isFirst();
}
//抽象聚合类Good
public interface Good{
AbstactIterator creatIterator();
}
//具体聚合类GoodName
ublic class GoodName implements Good {
private String [] goodname={};
public AbstactIterator creatIterator ()
{
return new MyIterator();
}
private class MyIterator implements AbstactIterator
{
private int PositionIndex = 0;
public void Btraversal ()
{
if(PositionIndex < goodname.length){
PositionIndex ++;
}
}
public void FTraversal ()
{
if(PositionIndex > 0){
PositionIndex --;
}
}
public vodi setPosition (int i)
{
PositionIndex = j;
}
public Object currentGoodName(){
{
return goodname[PositionIndex];
}
public boolean isLast(){
return PositionIndex == goodname.length;
}
public boolean isFirst(){
return PositionIndex == 0;
}
}
}
//辅助代码
<?xml version = "1.0">
<config>
<className>GoodName</className>
</config>
//客户端测试类
public class Client {
public static void display(GoodName) {
AbstactIterator i = good.createIterator();
while(!isLast()){
i.Btraversal();
}
}
public static void reverseDisplay(Good good){
AbstactIterator i = good.creatIterator();
i.currentGoodName(goodname.length);
while(!i.isFirst()){
i.FTraversal();
}
}
public static void main(String a[]){
Good good;
good = (Good)XMLUtil.getBean();
display(good);
reverseDisplay(good);
}
}
2、要求:某公司欲开发一个基于Windows平台的公告板系统,系统提供主菜单(Menu)其中有菜单项(MenuItem)。通过Menu类的addMenuItem()方法增加菜单项。菜单项的打开方法是click(),每个菜单项包含一个抽象命令类,具体命令类包括OpenCommand()、CreateCommand()、EditCommand()等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen())的open()、create()、edit())等方法。使用Command模式来设计。
类图如下:
代码如下:
//接受者BoardScreen类
public class BoardScreen {
public void open(){}
public void creat(){}
public void edit(){}
}
//抽象命令AbstratCommand类
public interface AbstratCommand {
public void execute();
}
//具体命令OpenCommand类
public class OpenCommand implements AbstractCommand {
private BoardScreen bo;
public OpenCommand (){
bo = new OpenCommand;
}
public void execute (){
bo.open();
}
}
//具体命令CreatCommand类
public class CreatCommand implements AbstractCommand {
private BoardScreen bo;
public CreatCommand (){
bo = new OpenCommand;
}
public void execute (){
bo.creat();
}
}
//具体命令EditCommand类
public class EditCommand implements AbstractCommand {
private BoardScreen bo;
public OpenCommand (){
bo = new EditCommand;
}
public void execute (){
bo.open();
}
}
//调用者MenuItem类
public class MenuItem {
private AbstractCommand openCommand,creatCommand,editCommand;
public click (AbstractCommand openCommand,AbstractCommand creatCommand,AbstractCommand editCommand){
this.openCommand = openCommand;
this.creatCommand = creatCommand;
this.editCommand = editCommand;
}
public void open(){
openCommand.execute();
}
public void open(){
creatCommand.execute();
}
public void open(){
editCommand.execute();
}
}
//客户端测试类
public class Cline {
public static void main(String args[]){
AbstractCommand openCommand,creatCommand,editCommand;
openCommand = OpenCommand;
creatCommand = CreatCommand;
editCommand = EditCommand;
MenuItem me = new MenuItem(openCommand,creatCommand,editCommand);
me.open();
me.creat();
me.edit();
}
}
3、要求:某论坛系统欲增加一个虚拟聊天室,允许论坛会员通过该聊天室进行信息交流,普通会员(CommonMember)可以给其他会员发送文本信息,钻石会员(DiamondMember)可以给其他会员发送文本和图片信息。该聊天室可以对不雅字符进行过滤,如“TMD”等字符,还可以对发送图片大小进行控制。使用Mediator模式来设计。
类图如下:
代码如下:
//抽象类AbstractChatroom
public abstract class AbstractChatroom {
public abstract void register(Member member);
public abstract void sendText(String from,String to,String message);
public abstract void sendImage(String from,String to,String image);
}
//抽象同事Member类
public abstract class Member {
protected AbstractChatroom chatroom;
protected String name;
public Member (String name){
this.name = name;
}
public String getName (){
return name;
}
public void setName (String name){
this.name = name;
}
public AbstractChatroom getChatroom(){
return chatroom
}
public void setChatroom (AbstractChatroom chatroom){
this.chatroom = chatroom;
}
public abstract void sendText (String to,String message);
public abstract void sendImage(String to,String image);
public void receiveText(String from,String message){}
public void receiveImage(String from,String image){}
}
//具体中介者ChatGroup类
public class ChatGroup extends AbstractChatroom {
private Hashtable members = new Hasht();
public void register (Member member){
if(!member.contains(member)){
member.put(member.getName(),member);
member.setChatroom(this);
}
}
public void sendText (String from,String to,String message){
Member member = (Member)member.get(to);
String newMessage = message;
enwMessage = message.replaceAll("TMD","*");
member.receiveIext(from,newMessage);
}
public void sendImage (String from,String to,String image){
Member member = (Member)member.get(to);
if(image.length()>5){
System.out.print("图片太大无法上传");
}else{
member.receiveImage(from.image);
}
}
}
//具体同事CommonMember类
public class CommonMember extends Member {
public CommonMember(String name){
super(name);
}
public void sendText(String from,String to,String message){
chatroom.sendText(name ,to ,message);
}
public void sendImage(String from,String to,String image){
System.out.print("普通会员无法发送图片");
}
}
//具体同事DiamonMember类
public class DiamonMember extends Member {
public DiamonMember(String name){
super(name);
}
public void sendText(String from,String to,String message){
chatroom.sendText(name ,to ,message);
}
public void sendImage(String from,String to,String image){
chatroom.sendImage(name ,to ,image);
}
}
//测试类代码
public class Cline {
public static void main(String args[]){
AbstractChatroom happyChat = new ChatGroup();
Member member1,member2,member3;
member1 = new DiamonMember("张三");
member2 = new DiamonMember("李四");
member3 = new DiamonMember("王五");
happyChat.register(member1);
happyChat.register(member2);
happyChat.register(member3);
member1.sendText("hello");
member2.sendText("hi");
member3.sendImage("张三","haizeiwang");
}
}
4、要求:设计一个网上书店,对系统中所有的计算机类图书(ComputerBook)每本都有10%的折扣,语言类图书(LanguageBook)每本都有2元的折扣,小说类图书(NovelBook)每100元有15元的折扣。使用Strategy模式来设计。
类图如下:
代码如下:
//抽象策略Dazhe类
publio interface Dazhe {
public abstract Dazhe(int num,int n);
}
//具体策略LangageBook类
public class LangageBook implement Dazhe{
public Dazhe (int num.int n){
this.num = num;
num = num - 2*n;
return num;
}
}
//具体策略ComputerBook类
public class ComputerBook implement Dazhe{
public Dazhe (int num.int n){
this.num = num;
num = num *0.9;
return num;
}
}
//具体策略NovelBook类
public class NovelBook implement Dazhe{
public Dazhe (int num.int n){
this.num = num;
this.n = n;
int i;
num = num * n;
i = num / 100;
num = num - 15*n;
return num;
}
}
//配置文件
<?xml version = "1.0"?>
<config>
<className>ComputerBook</className>
</config>
//测试类
public class Cline {
public static void main(String args[]){
int num = 40;//书本单价
int n = 2;//书本数量
int count;//总价
ArrayHandle ah = new ArrayHandle();
Dazhe dazhe;
dazhe = (Dazhe)xmlUtil.getBean();
ah.setDazhe(dazhe);
count = ah.dazhe(num,n);
System.out.print("共付款:"+count);
}
}