Unity实现设计模式——访问者模式

Unity实现设计模式------访问者模式

访问者模式(Vistor Pattern)是一种将数据结构和数据操作分离的设计模式。是指封装一些作用于某种数据结构中的各种元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新操作。

访问者模式的基本思想是针对系统中拥有的某些固定类型的对象结构(元素),在其内部提供一个accept()方法用来接受访问者对象的访问。不同的访问者对同一元素的访问内容不同,所以使得相同的元素可以产生不同的元素结果。就是说不同的访问者访问同一个对象里面的不同属性,产生了不同的结果。

下面使用不同的访问者可以分别调整收入和假期

1.Element

csharp 复制代码
        abstract class Element
        {
            public abstract void Accept(IVisitor visitor);
        }

2.Employee 职员

csharp 复制代码
class Employee : Element
        {
            private string _name;
            private double _income;
            private int _vacationDays;

            // Constructor
            public Employee(string name, double income,
              int vacationDays)
            {
                this._name = name;
                this._income = income;
                this._vacationDays = vacationDays;
            }

            // Gets or sets the name
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            // Gets or sets income
            public double Income
            {
                get { return _income; }
                set { _income = value; }
            }

            // Gets or sets number of vacation days
            public int VacationDays
            {
                get { return _vacationDays; }
                set { _vacationDays = value; }
            }

            public override void Accept(IVisitor visitor)
            {
                visitor.Visit(this);
            }
        }

3.IVisitor 访问者接口

csharp 复制代码
        interface IVisitor
        {
            void Visit(Element element);
        }

4.IncomeVisitor 收入访问者

csharp 复制代码
        class IncomeVisitor : IVisitor
        {
            public void Visit(Element element)
            {
                Employee employee = element as Employee;

                // Provide 10% pay raise
                employee.Income *= 1.10;

                Debug.Log(string.Format("{0} {1}'s new income: {2:C}",
                  employee.GetType().Name, employee.Name,
                  employee.Income));
            }
        }

5.VacationVisitor 假期访问者

csharp 复制代码
        class VacationVisitor : IVisitor
        {
            public void Visit(Element element)
            {
                Employee employee = element as Employee;

                // Provide 3 extra vacation days
                employee.VacationDays += 3;
                Debug.Log(string.Format("{0} {1}'s new vacation days: {2}",
                  employee.GetType().Name, employee.Name,
                  employee.VacationDays));
            }
        }

6.Employees 职员集合

csharp 复制代码
        class Employees
        {
            private List<Employee> _employees = new List<Employee>();

            public void Attach(Employee employee)
            {
                _employees.Add(employee);
            }

            public void Detach(Employee employee)
            {
                _employees.Remove(employee);
            }

            public void Accept(IVisitor visitor)
            {
                foreach (Employee e in _employees)
                {
                    e.Accept(visitor);
                }
            }
        }

7.不同的职员子类

csharp 复制代码
class Clerk : Employee
        {
            // Constructor
            public Clerk()
              : base("Hank", 25000.0, 14)
            {
            }
        }

        class Director : Employee
        {
            // Constructor
            public Director()
              : base("Elly", 35000.0, 16)
            {
            }
        }

        class President : Employee
        {
            // Constructor
            public President()
              : base("Dick", 45000.0, 21)
            {
            }
        }

8.测试

csharp 复制代码
        void Start()
        {
            // Setup employee collection
            Employees e = new Employees();
            e.Attach(new Clerk());
            e.Attach(new Director());
            e.Attach(new President());

            // Employees are 'visited'
            e.Accept(new IncomeVisitor());
            e.Accept(new VacationVisitor());
        }

就此为止23种设计模式中的11种行为模式介绍完毕!下面介绍其余的结构模式。

相关推荐
Asort3 小时前
JavaScript设计模式(二十一)——策略模式:灵活算法的艺术与实战
前端·javascript·设计模式
一只一只15 小时前
Unity 3D笔记(进阶部分)——《B站阿发你好》
笔记·3d·unity·游戏引擎
紫荆鱼16 小时前
设计模式-命令模式(Command)
c++·后端·设计模式·命令模式
杯莫停丶20 小时前
设计模式之:装饰器模式
java·设计模式·装饰器模式
yi碗汤园1 天前
【一文了解】八大排序-冒泡排序、选择排序
开发语言·前端·算法·unity·c#·1024程序员节
shi57831 天前
设计模式之 状态机 C#范例
设计模式·状态模式
qq_205279051 天前
unity PS 给图片换透明底图,去除白色底图
unity
Asort1 天前
JavaScript设计模式(二十)——状态模式 (State):复杂状态管理的优雅解决方案
前端·javascript·设计模式
向宇it1 天前
【推荐100个unity插件】unity易于使用模块化设计的天空、体积云和天气系统——Enviro 3
游戏·3d·unity·c#·游戏引擎
紫荆鱼1 天前
设计模式-备忘录模式(Memento)
c++·后端·设计模式·备忘录模式