行为型设计模式实验

3-1 职责链模式实例之在线文档帮助系统
某公司欲开发一个软件系统的在线文档帮助系统,用户可以在任何一个查询环境中输入查询关键字,如果当前查询环境下没有相关内容,则系统会将查询按照一定的顺序转发给其他查询环境。设查询环境如下:JavaSearchContextSQLSearchContext
UMLSearchContext

3-2 命令模式实例之公告板系统
某软件公司欲开发一个基于Windows平台的公告板系统。系统提供一个主菜单(Menu),在主菜单中包含了一些菜单项(MenuItem),可以通过Menu类的addMenuItem()方法增加菜单项。菜单项的主要方法是click(),每一个菜单项包含一个抽象命令类,具体命令类包括OpenCommand(打开命令)、CreateCommand(新建命令)、EditCommand(编辑命令)等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen)的open()create()edit()等方法。现使用命令模式设计该系统,使得MenuItem类与BoardScreen类的耦合度降低,绘制类图并编程模拟实现。
3-3 迭代器模式实例之商品名称遍历
某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(前向和后向)遍历。绘制类图并编程实现。
3-4 中介者模式实例之虚拟聊天室
虚拟聊天室实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1 新的具体聊天室中发送的图片大小不得超过20
2 新的具体聊天室中发送的文字信息的长度不得超过100个字符,提供更强大的不雅字符过滤功能(如可过滤TMD等字符)。
3 新的具体会员类可以发送图片信息和文字信息。
4 新的具体会员类在发送文本信息时,可以在信息后加上发送时间,格式为:文本信息(发送时间)。
修改客户端测试类,注意原有系统类库代码和客户端代码的改变。

3-5 观察者模式实例之股票变化
某在线股票软件需要提供如下功能:当股票购买者所购买的某支股票价格变化幅度达到5%时,系统将自动发送通知(包括新价格)给购买股票的股民。现使用观察者模式设计该系统,绘制类图并编程模拟实现。
3-6状态模式实例之银行转账
在某银行系统中,我们定义了账户的三种状态:
1)如果账户(Account)中余额(balance)大于等于0,此时账户的状态为绿色(GreenState),即正常状态,表示既可以向该账户存款(deposit)也可以向从该账户取款(whithdraw)
2)如果该账户中余额小于0,并且大于等于-1000,则该账户的状态为黄色(YellowState),即欠费状态,此时既可以向该账户存款也可以从该账户取款。
3)如果该账户余额小于-1000,那么该账户的状态为红色(RedState),即透支状态,此时用户只能向该账户存款,不能在从中取款。
现在用状态模拟来实现状态的转化问题,用户只要执行简单的存款和取款操作,系统根据数量自动转换到相应的状态。

3-7策略模式实例之电影票打折
某电影院售票系统为不同类型的用户提供了不同的打折方式(Discount),学生凭学生证可以享受8折优惠(StudentDiscount),儿童可以享受减免10元的优惠(ChildrenDiscount)VIP用户享受半折优惠还可以进行积分(VIPDiscount)。使用策略模式设计该系统,结合场景绘制相应的类图并编码实现。

3-8模板方法实例之数据库操作
对数据库的操作一般包括连接、打开、使用、关闭等步骤,在数据库操作模板中我们定义了connDB()openDB()useDB()closeDB()四个方法分别对应这四个步骤。对于不同类型的数据库(SQLServerOracle),其操作步骤都一致,只是连接数据库方法有所区别,现使用模板方法对其进行设计。

 

实验三:

3-1 职责链模式实例之在线文档帮助系统

 

 

 

 

代码如下:

package zhizelian;

public class SearchKey {

private String oneKey;

public SearchKey(String oneKey){

this.oneKey=oneKey;

}

public void setKey(String oneKey){

this.oneKey=oneKey;

}

public String getKey(){

return(this.oneKey);

}

}

package zhizelian;

public abstract class Search {  

    protected Search successor;  

    public void setSuccessor(Search successor) {  

        this.successor = successor;  

    }  

    public abstract void handleRequest(SearchKey key);  

}  

 package zhizelian;

 public class Java extends Search{

    public void handleRequest(SearchKey key) {

        String key1 = "java";

        if((key.getKey()).contains(key1))

            System.out.println("查询Java");

        else

            if(this.successor != null) this.successor.handleRequest(key);

    }

}

package zhizelian;

public class UML extends Search{

    public void handleRequest(SearchKey key) {

        String key1 = "UML";

        if((key.getKey()).contains(key1))  {

         System.out.println("您搜索的内容在Java中不存在");

            System.out.println("查询UML");  

        }

        else  

          if(this.successor != null)

              this.successor.handleRequest(key);

    }

} package zhizelian;

public class SQL extends Search{

    public void handleRequest(SearchKey key) {

        String key1 = "SQL";

        if((key.getKey()).contains(key1)) {

         System.out.println("您搜索的内容在JavaUML不存在");

            System.out.println("查询SQL");  

        }

        else  

         System.out.println("您搜索的内容不存在");

    }

} package zhizelian;

public class Test {

   public static void main(String[] args) {

         Search objJava,objSQL,objUML;

         objJava = new Java();  

         objSQL = new SQL();  

         objUML = new UML();

        objJava.setSuccessor(objUML);  //设置下一级,这是体现职责链的代码

        objUML.setSuccessor(objSQL);  

        System.out.println("输入xjavas");

        SearchKey key1 = new SearchKey("xjavas");  

        objJava.handleRequest(key1);   

        System.out.println("输入UMLS");

        SearchKey key2 = new SearchKey("UMLS");  

        objJava.handleRequest(key2);   

        System.out.println("输入YYSQLS");

        SearchKey key3 = new SearchKey("YYSQLS");  

        objJava.handleRequest(key3);

        System.out.println("输入C++");

        SearchKey key4 = new SearchKey("C++");  

        objJava.handleRequest(key4);  

     }

 }

3-6银行转账

实例场景:状态实例模式之银行账户,我们定义了账户的三种状态:①如果账户中余额大于等于0,此时账户的状态为绿色,即为正常状态,表示既可以想账户存款,也可以从该账户取款。②如果账户中余额小于0,并且大于-1000,则账户的状态为黄色,即为欠费状态,表示既可以取款也可以存款③如果账户中余额小于-1000,那么账户的状态为红色,即为透支状态,此时用户只能向该账户存款,不能取款。

 

实例类图:

           

           

 

实例代码:

 

//状态类

 

[java] view plain copy

/**

 * 抽象状态类

 * @author fly

 *

 */  

public abstract class AccountState{  

  

    protected Account account;  

    protected double balance;  

      

    public abstract void deposit(double amount);  

    public abstract void withdraw(double amount);  

    public abstract void stateCheck();  

}  

[java] view plain copy

/**

 * 绿色状态类:具体状态类

 * @author fly

 *

 */  

public class GreenState extends AccountState{  

  

    public GreenState(AccountState state) {  

        // TODO Auto-generated constructor stub  

        this.balance =state.balance;  

        this.account = state.account;  

    }  

      

    public GreenState(double balance,Account account) {  

        // TODO Auto-generated constructor stub  

        this.balance = balance;  

        this.account = account;  

    }  

      

    @Override  

    public void deposit(double amount) {  

        // TODO Auto-generated method stub  

        this.balance += amount;  

        stateCheck();  

    }  

  

    @Override  

    public void withdraw(double amount) {  

        // TODO Auto-generated method stub  

        this.balance -= amount;  

        stateCheck();  

    }  

  

    @Override  

    public void stateCheck() {  

        // TODO Auto-generated method stub  

        if(balance>=-1000&&balance<0){  

            account.setState(new YellowState(this));  

        }else if(balance<-1000){  

            account.setState(new RedState(this));  

        }  

    }  

  

}  

[java] view plain copy

/**

 * 黄色状态类:具体状态类

 * @author fly

 *

 */  

public class YellowState extends AccountState{  

  

    public YellowState(AccountState state) {  

        // TODO Auto-generated constructor stub  

        this.balance = state.balance;  

        this.account = state.account;  

    }  

      

    @Override  

    public void deposit(double amount) {  

        // TODO Auto-generated method stub  

        this.balance += amount;  

        stateCheck();  

    }  

  

    @Override  

    public void withdraw(double amount) {  

        // TODO Auto-generated method stub  

        this.balance -= amount;  

        stateCheck();  

    }  

  

    @Override  

    public void stateCheck() {  

        // TODO Auto-generated method stub  

        if(balance>=0){  

            account.setState(new GreenState(this));  

        }else if(balance<-1000){  

            account.setState(new RedState(this));  

        }  

    }  

  

}  

 

[java] view plain copy

/**

 * 红色状态类:具体状态类

 * @author fly

 *

 */  

public class RedState extends AccountState{  

  

    public RedState(AccountState state) {  

        // TODO Auto-generated constructor stub  

        this.balance = state.balance;  

        this.account = state.account;  

    }  

      

    @Override  

    public void deposit(double amount) {  

        // TODO Auto-generated method stub  

        this.balance += amount;  

        stateCheck();  

    }  

  

    @Override  

    public void withdraw(double amount) {  

        // TODO Auto-generated method stub  

        System.out.println("账户被冻结,取款失败");  

    }  

  

    @Override  

    public void stateCheck() {  

        // TODO Auto-generated method stub  

        if(balance>=0){  

            account.setState(new GreenState(this));  

        }else if(balance>=-1000){  

            account.setState(new YellowState(this));  

        }  

    }  

  

}  

//银行账户

[java] view plain copy

/**

 * 银行账户:环境类

 *  

 * @author fly

 *  

 */  

class Account {  

  

    private AccountState state;  

    private String owner;  

  

    public Account(String owner,double init) {  

        // TODO Auto-generated constructor stub  

        this.owner = owner;  

        this.state = new GreenState(init, this);  

        System.out.println(this.owner+"开户,初始金额为"+init);  

        System.out.println("--------------------------------------------------");  

    }  

      

    public void setState(AccountState state){  

        this.state = state;  

    }  

  

    public void deposit(double amount){  

        System.out.println(this.owner+"存款"+amount);  

        state.deposit(amount);  

        System.out.println("现金余额为"+state.balance);  

        System.out.println("现在账户状态为"+state.getClass().getName());  

        System.out.println("--------------------------------------------------");  

    }  

      

    public void withdraw(double amount){  

        System.out.println(this.owner+"取款"+amount);  

        state.withdraw(amount);  

        System.out.println("现金余额为"+state.balance);  

        System.out.println("现在账户状态为"+state.getClass().getName());  

        System.out.println("--------------------------------------------------");  

    }  

}  

//客户端测试

[java] view plain copy

public class Client {  

      

    public static void main(String[] args) {  

          

        Account account = new Account("fly", 5.0);  

        account.deposit(100);  

        account.withdraw(200);  

        account.deposit(1000);  

        account.withdraw(2000);  

        account.withdraw(100);  

    }  

}  

 

3-3迭代器商品遍历

实例类图:

 

实例代码:

 

 

实例代码:

 

//地带器代码如下:

 

[java] view plain copy

public interface AbstractIterator {  

      

    public void next();  

    public boolean isLast();  

    public void previous();  

    public boolean isFirst();  

    public String getNextItem();  

    public String getPreviousItem();  

}  

class MyIterator implements AbstractIterator{  

  

    private String[] productsName;  

    private int index1;  

    private int index2;  

      

    public MyIterator(AbstractProductList list) {  

        // TODO Auto-generated constructor stub  

        productsName = list.getProductsName();  

        index1 = 0;  

        index2 = productsName.length - 1;  

    }  

      

    @Override  

    public void next() {  

        // TODO Auto-generated method stub  

        if(index1

            index1++;  

        }  

    }  

  

    @Override  

    public boolean isLast() {  

        // TODO Auto-generated method stub  

        return (index1 == productsName.length);  

    }  

  

    @Override  

    public void previous() {  

        // TODO Auto-generated method stub  

        if (index2>-1) {  

            index2--;  

        } else {  

  

        }  

    }  

  

    @Override  

    public boolean isFirst() {  

        // TODO Auto-generated method stub  

        return index2==-1;  

    }  

  

    @Override  

    public String getNextItem() {  

        // TODO Auto-generated method stub  

        return productsName[index1];  

    }  

  

    @Override  

    public String getPreviousItem() {  

        // TODO Auto-generated method stub  

        return productsName[index2];  

    }  

      

      

}  

//列表代码如下:

[java] view plain copy

/**

 * 抽象商品集合:抽象聚合类

 * @author fly

 *

 */  

abstract class AbstractProductList {  

      

    private String[] productsName;  

      

    public AbstractProductList(String[] productsName) {  

        // TODO Auto-generated constructor stub  

        this.productsName = productsName;  

    }  

      

    public String[] getProductsName(){  

        return this.productsName;  

    }  

      

    public abstract AbstractIterator getIterator();  

}  

class ProductList extends AbstractProductList{  

  

    public ProductList(String[] productsName) {  

        // TODO Auto-generated constructor stub  

        super(productsName);  

    }  

  

    @Override  

    public AbstractIterator getIterator() {  

        // TODO Auto-generated method stub  

        return new MyIterator(this);  

    }  

}  

//客户端测试

[java] view plain copy

public class Client {  

      

    public static void main(String[] args) {  

          

        String[] pNames = {"ThinkPad电脑","Tissot手表","iPhone手机","LV手提包"};  

        AbstractIterator iterator;  

        AbstractProductList list;  

        list = new ProductList(pNames);  

        iterator = list.getIterator();  

        while (!iterator.isLast()) {  

            System.out.println(iterator.getNextItem());  

            iterator.next();  

        }  

        System.out.println("----------------------------------------------------");  

        while (!iterator.isFirst()) {  

            System.out.println(iterator.getPreviousItem());  

            iterator.previous();  

        }  

    }  

}  

 

你可能感兴趣的:(行为型设计模式实验)