设计模式之—访问者模式VisitorPattern
访问者模式:表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变个元素的类的前提下定义作用于这些元素的新操作。
适用于数据结构相对稳定的系统,如下示例的男人和女人。男人和女人是固定的
示例的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相关推荐
- Java设计模式(访问者模式-迭代器模式-观察者模式-中介者模式)
Java设计模式Ⅶ 1.访问者模式 1.1 访问者模式概述 1.2 代码理解 2.迭代器模式 2.1 迭代器模式概述 2.2 代码理解 3.观察者模式 3.1 观察者模式概述 3.2 代码理解 4.中 ...
- 北风设计模式课程---访问者模式(Visitor)
北风设计模式课程---访问者模式(Visitor) 一.总结 一句话总结: 设计模式是日常问题的经验总结方案,所以学好设计模式对日常出现的问题可以有很好的解决. 访问者设计模式有点神似 抽象工厂模式, ...
- 设计模式之访问者模式(Visitor)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 设计模式复习-访问者模式
VisitorMode.h#pragma once #include<list> #include<string> #include<iostream> using ...
- 【设计模式】访问者模式 ( 简介 | 适用场景 | 优缺点 | 代码示例 )
文章目录 一.访问者模式简介 二.访问者模式 适用场景 三.访问者模式 优缺点 四.访问者模式 与 迭代器模式 五.代码示例 1.Game 父类 ( 被访问者 ) 2.VipGame 收费游戏 ( 被 ...
- C++设计模式之访问者模式
访问者模式 在GOF的<设计模式:可复用面向对象软件的基础>一书中对访问者模式是这样说的:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的 ...
- 设计模式:访问者模式(Vistor)
欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...
- java的string访问某个元素_C#深究.net常用的23种设计模式之访问者模式(Vistor Pattern)...
一.引言 在上一篇博文中分享了责任链模式,责任链模式主要应用在系统中的某些功能需要多个对象参与才能完成的场景.在这篇博文中,我将为大家分享我对访问者模式的理解. 二.访问者模式介绍 2.1 访问者模式 ...
- Android设计模式之——访问者模式
一.介绍 访问者模式是一种将数据操作与数据结构分离的设计模式,它是<设计模式>中23种设计模式中最复杂的一个,但它的使用频率并不高,正如<设计模式>的作者GOF对访问者模式的描 ...
最新文章
- Windows数据类型探幽——千回百转你是谁?(2)
- python sqlalchemy中文手册-基于Python的SQLAlchemy的操作
- 什么是传感器融合?我们从“盲人摸象”讲起……
- PHP成为首个在内核中嵌入加密库的编程语言
- vue调用手机相机相册_详解Vue调用手机相机和相册以及上传
- 新研究:长寿又健康的秘诀
- micropython stm32f429_[MicroPython]STM32F407开发板驱动OLED液晶屏
- plt.title() 把标题置于图像下方
- Windows Phone 7开发人员向导已经发布
- 休闲甜品店创业计划书_甜品店创业计划书
- xp 安装程序在计算机中识别出下列大容量存储设备,大容量存储控制器驱动程序安装步骤[图形]...
- HTML+CSS大作业 (水果之家10个网页)
- Python 程序员需要知道的 30 个技巧
- 高中关于人工智能方面的课题_人工智能课题及其认识意义.doc
- [hdu6148][Valley Numer]
- Unity API - A
- html5页面正文内容标签,HTML5 结构标签
- EDGE浏览器关闭网址栏自动补全
- TIA博途_OB组织块的功能和使用方法介绍
- Self-supervised
热门文章
- 通过关闭UseDNS和GSSAPIAuthentication选项加速SSH登录
- python搜索引擎和爬虫框架介绍
- 使用一些宏跟踪调试__LINE__ __FILE__ __DATE__ __TIME__ __STDC__
- 【转】jquery ui中文说明(使用方法)
- stm32经典笔试题_嵌入式面试经典30问
- hive 配置用户名_hive的用户和用户权限
- c post请求网页_Python使用urllib2抓取网页
- FPGA可综合语句建立原则
- STM32驱动LCD实战
- 汉化)称号插件.php,[管理|信息][UD]NameTags——基于权限的称号插件,兼容计分板,GUI显示[1.7.10-1.12.2]...