zoukankan      html  css  js  c++  java
  • 第二节:访问者模式——访问者模式应用实例

    一、访问者模式应用实例

      1、应用实例要求

        将人分为男人和女人,对歌手进行测评,当看完某个歌手表演后,得到他们对该歌手不同的评价(评价有不同种类:比如成功、失败),使用访问者模式完成实现。

      2、思路分析和图解

        

    二、代码实现

      抽象访问者:

     1 /**
     2  * 访问者
     3  */
     4 public abstract class Action {
     5 
     6     /**
     7      * 得到男性的测评
     8      */
     9     public abstract void getManResult(Man man);
    10 
    11     /**
    12      * 得到女性的评测
    13      */
    14     public abstract void getWomanResult(Woman woman);
    15 }

      具体访问者:

     1 public class Success extends Action{
     2     @Override
     3     public void getManResult(Man man) {
     4         System.out.println("男人给的评价是该歌手很成功");
     5     }
     6 
     7     @Override
     8     public void getWomanResult(Woman woman) {
     9         System.out.println("女人给的评价是该歌手很成功");
    10     }
    11 }
    12 ------------------------------------------------------
    13 public class Fail extends Action{
    14     @Override
    15     public void getManResult(Man man) {
    16         System.out.println("男人给的评价是该歌手失败");
    17     }
    18 
    19     @Override
    20     public void getWomanResult(Woman woman) {
    21         System.out.println("女人给的评价是该歌手失败");
    22     }
    23 }

      Element 抽象类:

    1 public abstract class Person {
    2 
    3     /**
    4      * 提供一个方法,让访问者可以访问
    5      */
    6     public abstract void accept(Action action);
    7 }

      ConcreteElement 具体实现类:

     1 /**
     2  * 说明:
     3  *  1、这里我们使用到了双分派,即首先在客户端程序中将具体的状态作为参数
     4  *  传递到了 Man 中(第一次分派),
     5  *
     6  *  2、然后 Man 类中调用作为参数的 "具体方法" 中方法getManResult,
     7  *  同时将自己(this)作为参数传入(第二次分派)
     8  */
     9 public class Man extends Person{
    10     @Override
    11     public void accept(Action action) {
    12         action.getManResult(this);
    13     }
    14 }
    15 
    16 ---------------------------------------------------
    17 public class Woman extends Person{
    18     @Override
    19     public void accept(Action action) {
    20         action.getWomanResult(this);
    21     }
    22 }

      ObjectStructure 数据结构:

     1 /**
     2  * 数据结构,管理很多人(Man,Woman,,,)
     3  */
     4 public class ObjectStructure {
     5 
     6     /**
     7      * 维护了一个集合
     8      */
     9     public List<Person> persons = new LinkedList<>();
    10 
    11     /**
    12      * 增加到 list
    13      */
    14     public void attache(Person person) {
    15         persons.add(person);
    16     }
    17 
    18     /**
    19      * 移除
    20      */
    21     public void detach(Person person) {
    22         persons.remove(person);
    23     }
    24 
    25     /**
    26      * 提示测评情况
    27      */
    28     public void display(Action action) {
    29         for (Person p : persons) {
    30             p.accept(action);
    31         }
    32     }
    33 }

      客户端:

     1 public class Client {
     2     public static void main(String[] args) {
     3         //创建 ObjectStructure
     4         ObjectStructure objectStructure = new ObjectStructure();
     5 
     6         objectStructure.attache(new Man());
     7         objectStructure.attache(new Woman());
     8 
     9         //成功
    10         Success success = new Success();
    11         objectStructure.display(success);
    12 
    13         Fail fail = new Fail();
    14         objectStructure.display(fail);
    15 
    16         int size = objectStructure.persons.size();
    17         for (int i = 0; i < size; i++) {
    18             if (i % 2 == 0) {
    19                 objectStructure.persons.get(i).accept(success);
    20             } else {
    21                 objectStructure.persons.get(i).accept(fail);
    22             }
    23         }
    24     }
    25 }

     

     

  • 相关阅读:
    Haskell Types与Typeclasses
    Haskell Tuple相关总结
    Haskell List相关操作
    Emacs 常用快捷键
    Emacs 参考资料
    Haskell Platform (windows)
    生成zip压缩包
    递归复制一个文件
    写表格
    读表格
  • 原文地址:https://www.cnblogs.com/niujifei/p/14395480.html
Copyright © 2011-2022 走看看