【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中定义: "定义一个程序设计语言所需要的语句,并提供解释来解析(执行)该语言." 传统上,执行程序代码通常通过两种方式 第一种:编译程序 第二种:解释器 常见的使用解释器的程 ...
随机推荐
- Zend Framework在windows下的安装
1:首先需要下载安装PHP的依赖管理工具Composer 详情去http://docs.phpcomposer.com/了解 下载链接: https://getcomposer.org/downloa ...
- UVA - 10723 类似LCS
思路:dp(i, j)表示第一个串前i个字符和第二个串前j个字符需要的最短字符串长度,cnt(i, j)表示第一个串前i个字符和第二个串前j个字符需要的最短字符串的个数. 转移方程: if(s1[i] ...
- tox环境安装
ubuntu 下安装tox环境 1.apt-get install pip 2.pip install tox 3.git git clone https://github.com/openstack ...
- 【mysql】mysql基本操作
mysql基本操作 1.mysql表复制 mysql 表结构的复制 create table t2 like t2 mysql 表数据的复制 insert into t2 select * from ...
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- ffmpeg在am335x上的移植
交叉编译工具:arm-linux-gcc 一.先下载一下文件 1. yasm-1.2.0.tar.gz 2. x264-snapshot-20140424-2245.tar.bz2 3. xvidco ...
- linux 安装沙盒virtualenv 、virtualenvwrapper
1.沙盒安装命令: 最新版本:sudo easy_install virtualenv或者sudo apt-get install virtualenv 指定版本:pip install virtua ...
- MySQL中information_schema数据库的内容
大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个information_schema数据库. information_schema数据库是做什么用的呢,使用WordPress博客 ...
- 三:Linux 的基本命令、
Ubuntu切换用户 su root sudo passwd root 使用管理员提权修改root 登录密码 连续输入两次即可..... 重置root 用户密码 例:当前登录用户为:ubuntu,但是 ...
- RobotFramework下的http接口自动化Create Http Context关键字的使用
要想使用HttpLibrary,Create Http Context 关键字的作用相当于是创建了一个http 调用的环境,是必不可少的一个关键字. Create Http Context 关键字需要 ...