GoF中定义:

“定义一个能够在一个对象结构中对于所有元素执行的操作。访问者让你可以定义一个新的操作,而不必更改到被操作元素的类接口。”

暂时没有完全搞明白

直接上代码

//访问者接口
public abstract class IShapeVisitor {
public virtual void VisitSphere(Sphere theShpere) { }
public virtual void VisitCube(Cube theCube) { }
public virtual void VisitCylinder(Cylinder theCylinder) { }
} //形状容器
public class ShapeContainer {
List<IShape> m_Shapes = new List<IShape>(); public ShapeContainer() { } public void AddShape(IShape theShape) {
m_Shapes.Add(theShape);
} public void RunVisitor(IShapeVisitor theVisitor) {
foreach (IShape theShape in m_Shapes) {
theShape.RunVisitor(theVisitor);
}
}
}
//形状的定义
public abstract class IShape {
public abstract void RunVisitor(IShapeVisitor theVisitor);
} //各形状的重新实现
public class Sphere : IShape {
public override void RunVisitor(IShapeVisitor theVisitor)
{
theVisitor.VisitSphere(this);
}
} public class Cube : IShape {
public override void RunVisitor(IShapeVisitor theVisitor)
{
theVisitor.VisitCube(this);
}
} public class Cylinder : IShape {
public override void RunVisitor(IShapeVisitor theVisitor)
{
theVisitor.VisitCylinder(this);
}
}
//绘图功能的Visitor
public class DrawVisitor : IShapeVisitor {
public override void VisitSphere(Sphere theShpere)
{
base.VisitSphere(theShpere);
Debug.Log("画Sphere");
} public override void VisitCube(Cube theCube)
{
base.VisitCube(theCube);
Debug.Log("画Cube");
} public override void VisitCylinder(Cylinder theCylinder)
{
base.VisitCylinder(theCylinder);
Debug.Log("画Cylinder");
}
}
//测试类
public class TestVisitor {
void UnitTest() {
ShapeContainer theShapeContainer = new ShapeContainer();
theShapeContainer.AddShape(new Sphere());
theShapeContainer.AddShape(new Cube());
theShapeContainer.AddShape(new Cylinder()); theShapeContainer.RunVisitor(new DrawVisitor());
}
}

文章整理自书籍《设计模式与游戏完美开发》 菜升达 著

【Unity与23种设计模式】访问者模式(Visitor)的更多相关文章

  1. 24种设计模式--访问者模式【Visitor Pattern】

    今天天气不错,绝对是晴空万里,骄阳似火呀,好,我们今天来讲访问者模式,我们在前面讲了组合模式和迭代器模式,通过组合模式我们能够把一个公司的人员组织机构树搭建起来,给管理带来非常大的便利,通过迭代器模式 ...

  2. 设计模式 -- 访问者模式(Visitor)

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: 初识访问者模 ...

  3. C#设计模式——访问者模式(Visitor Pattern)

    一.概述由于需求的改变,某些类常常需要增加新的功能,但由于种种原因这些类层次必须保持稳定,不允许开发人员随意修改.对此,访问者模式可以在不更改类层次结构的前提下透明的为各个类动态添加新的功能.二.访问 ...

  4. 大话设计模式--访问者模式 Visitor -- C++实现实例

    1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...

  5. php 23种设计模式 - 解释器模式

    给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子.角色:环境角色(PlayContent):定义解释规则的全局信息.抽象解释器(Empress):定义了部 ...

  6. php 23种设计模式 - 备忘录模式

    备忘录模式 备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象.备忘录模式属于行为型模式. 介绍 意图:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该 ...

  7. php 23种设计模式 - 命令模式

    命令模式 将一个请求封装为一个对象,从而使用户可用不同的请求对客户进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 命令模式以松散耦合主题为基础,发送消息.命令和请求,或通过一组处理程序发送 ...

  8. php 23种设计模式 - 迭代器模式

    迭代器模式 迭代器模式 (Iterator),又叫做游标(Cursor)模式.提供一种方法访问一个容器(Container)对象中各个元素,而又不需暴露该对象的内部细节. 当你需要访问一个聚合对象,而 ...

  9. 【Unity与23种设计模式】解释器模式(Interpreter)

    GoF中定义: "定义一个程序设计语言所需要的语句,并提供解释来解析(执行)该语言." 传统上,执行程序代码通常通过两种方式 第一种:编译程序 第二种:解释器 常见的使用解释器的程 ...

随机推荐

  1. Shell脚本查看linux系统性能瓶颈(转)

    Shell脚本查看linux系统性能瓶颈(转自:http://blog.51cto.com/lizhenliang/1687612) [root@test ~]# cat show_sys_info. ...

  2. 业余草分享 Spring Boot 2.0 正式发布的新特性

    就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...

  3. MFRC522

    https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/README.md https://github.com/mxgx ...

  4. UVA - 658 最短路

    思路:通过前后两种状态建立一条边,利用Dijsktra就可以做了. 注意利用二进制优化. AC代码 #include <cstdio> #include <cmath> #in ...

  5. hdu1698 Just a Hook 线段树

    共有Q个更新,每次更新给更新的区间一个标记,表示该区间是在哪一次被更新,最后统计答案是以最近被更新的值为答案. AC代码: #include<cstdio> const int maxn= ...

  6. React Native填坑之旅 -- 回归小插曲

    回归RN,非常开心啊! 在React Native 0.49.5上开发,直接遇到一个ios模拟器的问题.这个问题很简单就是Bundle URL not present. 在网上找了很多的解决方法,都不 ...

  7. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  8. 在SpringBoot中配置定时任务

    前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...

  9. DataCleaner第一章

    Part1. Introduction to DataCleaner 介绍DataCleaner |--What is data quality(DQ) 数据质量? |--What is data p ...

  10. DriverStudio开发PCI设备DMA数据传输

    DriverWizard向导可以创建基本的wDM驱动程序框架,包括总线类型,地址空间,中断源,DMA资源,以及IOCTL(i/o控制代码)的定义等等.详细情况可参看DriverStudio的帮助文档, ...