【Unity与23种设计模式】访问者模式(Visitor)
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)的更多相关文章
- 24种设计模式--访问者模式【Visitor Pattern】
今天天气不错,绝对是晴空万里,骄阳似火呀,好,我们今天来讲访问者模式,我们在前面讲了组合模式和迭代器模式,通过组合模式我们能够把一个公司的人员组织机构树搭建起来,给管理带来非常大的便利,通过迭代器模式 ...
- 设计模式 -- 访问者模式(Visitor)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------主要内容包括: 初识访问者模 ...
- C#设计模式——访问者模式(Visitor Pattern)
一.概述由于需求的改变,某些类常常需要增加新的功能,但由于种种原因这些类层次必须保持稳定,不允许开发人员随意修改.对此,访问者模式可以在不更改类层次结构的前提下透明的为各个类动态添加新的功能.二.访问 ...
- 大话设计模式--访问者模式 Visitor -- C++实现实例
1. 访问者模式: 表示一个作用于某对象结构中的和元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 访问者模式把数据结构和作用于结构上的操作之间的耦合脱开,使得操作集合可以 ...
- php 23种设计模式 - 解释器模式
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子.角色:环境角色(PlayContent):定义解释规则的全局信息.抽象解释器(Empress):定义了部 ...
- php 23种设计模式 - 备忘录模式
备忘录模式 备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象.备忘录模式属于行为型模式. 介绍 意图:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该 ...
- php 23种设计模式 - 命令模式
命令模式 将一个请求封装为一个对象,从而使用户可用不同的请求对客户进行参数化.对请求排队或记录请求日志,以及支持撤销的操作. 命令模式以松散耦合主题为基础,发送消息.命令和请求,或通过一组处理程序发送 ...
- php 23种设计模式 - 迭代器模式
迭代器模式 迭代器模式 (Iterator),又叫做游标(Cursor)模式.提供一种方法访问一个容器(Container)对象中各个元素,而又不需暴露该对象的内部细节. 当你需要访问一个聚合对象,而 ...
- 【Unity与23种设计模式】解释器模式(Interpreter)
GoF中定义: "定义一个程序设计语言所需要的语句,并提供解释来解析(执行)该语言." 传统上,执行程序代码通常通过两种方式 第一种:编译程序 第二种:解释器 常见的使用解释器的程 ...
随机推荐
- 轻松搭建Git服务器(Ubuntu)
搭建Git服务器 在远程仓库节中,我们讲了远程仓库实际上和本地仓库没啥不同,纯粹为了7x24小时开机并交换大家的修改. GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业 ...
- 在Centos环境下安装兼容Apache2.4高版本SVN服务
在阿里云Centos环境下,搭建PHP运行环境,PHP选择了php7版本,Mysql选择了5.7版本,Apache选择了2.4版本,在搭建SVN版本控制服务过程中出现了不兼容问题,当前环境下Apach ...
- SharedPreferences封装类
最近一直在读马伟奇老师的简书,给人以不一样的感觉,接下来的时间会做做笔记,毕竟好东西变成自己的才有用 原文地址SharedPreferencesUtils 依赖 dependencies { comp ...
- the c programing language 学习过程7
interact 互动 carriage运费运输 linefeed 换行 redirection改方向 interleaved交叉存取 adequate足够的 untouched原样的 specif ...
- 算法训练 K好数 数位DP+同余定理
思路:d(i,j)表示以i开头,长度为j的K好数的个数,转移方程就是 for(int u = 0; u < k; ++u) { int x = abs(i - u); if(x == 1) co ...
- Web/app端自动化测试对比
Web/app端自动化测试 做了一段时间的Android自动化测试,对比个人之前做的web端自动化测试,有一些感想.(由于个人接触的时间也不是太久,很多东西理解也并不深刻,先写下菜鸟时期的感想.) 区 ...
- django-装饰器实现PV统计
1.models层建立统计表 # 每日访问量统计 class Statistics(models.Model): pv = models.IntegerField(default=0) uv = mo ...
- Qemu,KVM,Virsh傻傻的分不清
当你安装了一台Linux,想启动一个KVM虚拟机的时候,你会发现需要安装不同的软件,启动虚拟机的时候,有多种方法: virsh start kvm命令 qemu命令 qemu-kvm命令 qemu-s ...
- Java求素数时出现错误
Java求素数时出现错误 1.具体错误如下 No enclosing instance of type Prime is accessible. Must qualify the allocation ...
- (三十二)虚拟机linux系统中安装firefox浏览器
这几天,第二轮项目迭代进入了尾声,同时也接到了上司领导交代下来的新任务,那就是要我们学习linux系统基础的知识,包括常用命令和web应用的部署问题,于是乎安装了虚拟机和linux操作系统. 我安装的 ...