访问者模式:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变个元素的类的前提下定义作用于这些元素的新操作。

适用于数据结构相对稳定的系统,如下示例的男人和女人。男人和女人是固定的

示例的UML图如下:

状态抽象类(Action)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{//状态类abstract class Action{public abstract void GetManReflect(Man man); //获取男人的反映public abstract void GetWomanReflect(Woman woman); //获取女人的反映
    }}

View Code

成功状态(Success)继承于状态抽象类(Action)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{//成功状态class Success : Action{public override void GetManReflect(Man man){Console.WriteLine("{0},{1}时,背后多半有个伟大的女人。",man.GetType().Name,this.GetType().Name);}public override void GetWomanReflect(Woman woman){Console.WriteLine("{0},{1}时,背后大多有一个不成功的男人。", woman.GetType().Name, this.GetType().Name);}}
}

View Code

失败状态(Failing)继承于状态抽象类(Action)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{class Failing :Action{public override void GetManReflect(Man man){Console.WriteLine("{0},{1}时,闷头喝酒,谁也不用劝。", man.GetType().Name, this.GetType().Name);}public override void GetWomanReflect(Woman woman){Console.WriteLine("{0},{1}时,眼泪汪汪,谁也劝不了", woman.GetType().Name, this.GetType().Name);}}
}

View Code

人类(Person)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{abstract class Person{protected string action;public string Action{get { return action; }set { action = value; }}/// <summary>/// 得出结论/// </summary>public abstract void GetConclusion(Action action); }
}

View Code

男人类(Man)继承于人类(Person)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{class Man:Person{public override void GetConclusion(Action action){action.GetManReflect(this);}}
}

View Code

女人类(Woman)继承于人类(Person)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{class Woman:Person{public override void GetConclusion(Action action){action.GetWomanReflect(this);}}
}

View Code

对象结构类(ObjectStruction) 双分派技术:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace VisitorPattern.CLASS
{//对象结构class ObjectStructure{private IList<Person> elements = new List<Person>();//增加public void Attach(Person element){elements.Add(element);}//移除public void Detach(Person element){elements.Remove(element);}//查看显示public void Display(Action action){foreach (Person person in elements){person.GetConclusion(action);}}}
}

View Code

测试类(TestMain)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using VisitorPattern.CLASS;
namespace VisitorPattern
{class TeatMain{static void Main(string[] args){ObjectStructure obj = new ObjectStructure();obj.Attach(new Man());obj.Attach(new Woman());Success su = new Success();obj.Display(su);Failing fa = new Failing();obj.Display(fa);Console.Read();}}
}

View Code

测试结果:

转载于:https://www.cnblogs.com/zxd543/p/3307659.html

设计模式之—访问者模式VisitorPattern相关推荐

  1. Java设计模式(访问者模式-迭代器模式-观察者模式-中介者模式)

    Java设计模式Ⅶ 1.访问者模式 1.1 访问者模式概述 1.2 代码理解 2.迭代器模式 2.1 迭代器模式概述 2.2 代码理解 3.观察者模式 3.1 观察者模式概述 3.2 代码理解 4.中 ...

  2. 北风设计模式课程---访问者模式(Visitor)

    北风设计模式课程---访问者模式(Visitor) 一.总结 一句话总结: 设计模式是日常问题的经验总结方案,所以学好设计模式对日常出现的问题可以有很好的解决. 访问者设计模式有点神似 抽象工厂模式, ...

  3. 设计模式之访问者模式(Visitor)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  4. 设计模式复习-访问者模式

    VisitorMode.h#pragma once #include<list> #include<string> #include<iostream> using ...

  5. 【设计模式】访问者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )

    文章目录 一.访问者模式简介 二.访问者模式 适用场景 三.访问者模式 优缺点 四.访问者模式 与 迭代器模式 五.代码示例 1.Game 父类 ( 被访问者 ) 2.VipGame 收费游戏 ( 被 ...

  6. C++设计模式之访问者模式

    访问者模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对访问者模式是这样说的:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的 ...

  7. 设计模式:访问者模式(Vistor)

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  8. java的string访问某个元素_C#深究.net常用的23种设计模式之访问者模式(Vistor Pattern)...

    一.引言 在上一篇博文中分享了责任链模式,责任链模式主要应用在系统中的某些功能需要多个对象参与才能完成的场景.在这篇博文中,我将为大家分享我对访问者模式的理解. 二.访问者模式介绍 2.1 访问者模式 ...

  9. Android设计模式之——访问者模式

    一.介绍 访问者模式是一种将数据操作与数据结构分离的设计模式,它是<设计模式>中23种设计模式中最复杂的一个,但它的使用频率并不高,正如<设计模式>的作者GOF对访问者模式的描 ...

最新文章

  1. Windows数据类型探幽——千回百转你是谁?(2)
  2. python sqlalchemy中文手册-基于Python的SQLAlchemy的操作
  3. 什么是传感器融合?我们从“盲人摸象”讲起……
  4. PHP成为首个在内核中嵌入加密库的编程语言
  5. vue调用手机相机相册_详解Vue调用手机相机和相册以及上传
  6. 新研究:长寿又健康的秘诀
  7. micropython stm32f429_[MicroPython]STM32F407开发板驱动OLED液晶屏
  8. plt.title() 把标题置于图像下方
  9. Windows Phone 7开发人员向导已经发布
  10. 休闲甜品店创业计划书_甜品店创业计划书
  11. xp 安装程序在计算机中识别出下列大容量存储设备,大容量存储控制器驱动程序安装步骤[图形]...
  12. HTML+CSS大作业 (水果之家10个网页)
  13. Python 程序员需要知道的 30 个技巧
  14. 高中关于人工智能方面的课题_人工智能课题及其认识意义.doc
  15. [hdu6148][Valley Numer]
  16. Unity API - A
  17. html5页面正文内容标签,HTML5 结构标签
  18. EDGE浏览器关闭网址栏自动补全
  19. TIA博途_OB组织块的功能和使用方法介绍
  20. Self-supervised

热门文章

  1. 通过关闭UseDNS和GSSAPIAuthentication选项加速SSH登录
  2. python搜索引擎和爬虫框架介绍
  3. 使用一些宏跟踪调试__LINE__ __FILE__ __DATE__ __TIME__ __STDC__
  4. 【转】jquery ui中文说明(使用方法)
  5. stm32经典笔试题_嵌入式面试经典30问
  6. hive 配置用户名_hive的用户和用户权限
  7. c post请求网页_Python使用urllib2抓取网页
  8. FPGA可综合语句建立原则
  9. STM32驱动LCD实战
  10. 汉化)称号插件.php,[管理|信息][UD]NameTags——基于权限的称号插件,兼容计分板,GUI显示[1.7.10-1.12.2]...