设计模式---访问者模式(DesignPattern_Visitor)

简介: 摘录自:设计模式与游戏完美开发十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity T...

摘录自:设计模式与游戏完美开发

十年磨一剑,作者将设计模式理论巧妙地融入到实践中,以一个游戏的完整实现呈现设计模式的应用及经验的传承 《轩辕剑》之父——蔡明宏、资深游戏制作人——李佳泽、Product Evangelist at Unity Technologies——Kelvin Lo、信仁软件设计创办人—— 赖信仁、资深3D游戏美术——刘明恺 联合推荐全书采用了整合式的项目教学,即以一个游戏的范例来应用23种设计模式的实现贯穿全书,让读者学习到整个游戏开发的全过程和作者想要传承的经验,并以浅显易懂的比喻来解析难以理解的设计模式,让想深入了解此领域的读者更加容易上手。


工程GitHub

VISITOR—情人节到了,要给每个MM送一束鲜花和一张卡片,可是每个MM送的花都要针对她个人的特点,每张卡片也要根据个人的特点来挑,我一个人哪搞得清楚,还是找花店老板和礼品店老板做一下Visitor,让花店老板根据MM的特点选一束花,让礼品店老板也根据每个人特点选一张卡,这样就轻松多了;

访问者模式:访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话,接受这个操作的数据结构可以保持不变。访问者模式适用于数据结构相对未定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由的演化。访问者模式使得增加新的操作变的很容易,就是增加一个新的访问者类。访问者模式将有关的行为集中到一个访问者对象中,而不是分散到一个个的节点类中。当使用访问者模式时,要将尽可能多的对象浏览逻辑放在访问者类中,而不是放到它的子类中。访问者模式可以跨过几个类的等级结构访问属于不同的等级结构的成员类。

using UnityEngine;
using System.Collections.Generic;

namespace DesignPattern_Visitor
{   
    // 固定的元素, 定义給Visitor存取的介面
    public abstract class Visitor
    {   
        // 可以写一个通用的函数名称但以用不同的参数来产生多样化方法
        public abstract void VisitConcreteElement( ConcreteElementA theElement);
        public abstract void VisitConcreteElement( ConcreteElementB theElement);


        // 或可以针对Element的子类做不同的执行操作
        public abstract void VisitConcreteElementA( ConcreteElementA theElement);
        public abstract void VisitConcreteElementB( ConcreteElementB theElement);

    }

    // 制定以Visitor物件当参数的Accept()介面
    public abstract class Element
    {   
        public abstract void Accept( Visitor theVisitor);       
    }

    // 元素A 
    public class ConcreteElementA : Element
    {
        public override void Accept( Visitor theVisitor)
        {
            theVisitor.VisitConcreteElement( this );
            theVisitor.VisitConcreteElementA( this );
        }

        public void OperationA()
        {
            Debug.Log("ConcreteElementA.OperationA");
        }
    }
    
    // 元素B
    public class ConcreteElementB : Element
    {
        public override void Accept( Visitor theVisitor)
        {
            theVisitor.VisitConcreteElement( this );
            theVisitor.VisitConcreteElementB( this );
        }

        public void OperationB()
        {
            Debug.Log("ConcreteElementB.OperationB");
        }
    }
    
    // 实例功能操作Visitor1
    public class ConcreteVicitor1 : Visitor
    {
        // 可以写一个通用的函数名称但以用不同的参数來产生多样化方法
        public override void VisitConcreteElement( ConcreteElementA theElement)
        {
            Debug.Log ("ConcreteVicitor1:VisitConcreteElement(A)");
        }
        public override void VisitConcreteElement( ConcreteElementB theElement)
        {
            Debug.Log ("ConcreteVicitor1:VisitConcreteElement(B)");
        }

        public override void VisitConcreteElementA( ConcreteElementA theElement)
        {
            Debug.Log ("ConcreteVicitor1.VisitConcreteElementA()");
            theElement.OperationA();
        }

        public override void VisitConcreteElementB( ConcreteElementB theElement)
        {
            Debug.Log ("ConcreteVicitor1.VisitConcreteElementB()");
            theElement.OperationB();
        }
    }

    // 实例功能操作Visitor2
    public class ConcreteVicitor2 : Visitor
    {
        // 可以写一个通用的函数名称但以用不同的参数来产生多样化方法
        public override void VisitConcreteElement( ConcreteElementA theElement)
        {
            Debug.Log ("ConcreteVicitor2:VisitConcreteElement(A)");
        }
        public override void VisitConcreteElement( ConcreteElementB theElement)
        {
            Debug.Log ("ConcreteVicitor2.VisitConcreteElement(B)");
        }

        public override void VisitConcreteElementA( ConcreteElementA theElement)
        {
            Debug.Log ("ConcreteVicitor2.VisitConcreteElementA()");
            theElement.OperationA();
        }
        
        public override void VisitConcreteElementB( ConcreteElementB theElement)
        {
            Debug.Log ("ConcreteVicitor2.VisitConcreteElementB()");
            theElement.OperationB();
        }   
    }
        
    // 用来储存所有的Element   
    public class ObjectStructure
    {
        List<Element> m_Context = new List<Element>();
    
        public  ObjectStructure()
        {
            m_Context.Add( new ConcreteElementA());
            m_Context.Add( new ConcreteElementB());
        }
        
        // 载入不同的Action(Visitor)來判断
        public void RunVisitor(Visitor theVisitor)
        {
            foreach(Element theElement in m_Context )
                theElement.Accept( theVisitor);
        }
    }
    
}
using UnityEngine;
using System.Collections;
using DesignPattern_Visitor;

public class VisitorTest : MonoBehaviour
{

    void Start()
    {
        UnitTest();
    }

   
    void UnitTest()
    {
        ObjectStructure theStructure = new ObjectStructure();

        // 將Vicitor遍历ObjectStructure里面各元表
        theStructure.RunVisitor(new ConcreteVicitor1());
        theStructure.RunVisitor(new ConcreteVicitor2());
    }
}
相关文章
|
2月前
|
设计模式 存储 uml
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
C++ 设计模式实战:外观模式和访问者模式的结合使用,派生类访问基类的私有子系统
29 1
|
5月前
|
设计模式
二十三种设计模式全面解析-访问者模式的高级应用和实践技巧
二十三种设计模式全面解析-访问者模式的高级应用和实践技巧
|
7月前
|
设计模式 算法 C++
设计模式之访问者模式(C++)
设计模式之访问者模式(C++)
|
4月前
|
设计模式 算法
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
39 1
设计模式 - 行为型模式_ 访问者模式Visitor Pattern
|
2月前
|
设计模式 算法 Java
【设计模式】访问者模式
【设计模式】访问者模式
|
7月前
|
设计模式 算法
行为型设计模式11-访问者模式
行为型设计模式11-访问者模式
27 1
|
4月前
|
设计模式 Java 容器
聊聊Java设计模式-访问者模式
访问者模式(Visitor Pattern)指将作用域某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作。
28 3
|
4月前
|
设计模式 Go 开发工具
Golang设计模式——09访问者模式
Golang设计模式——09访问者模式
420 0
|
4月前
|
设计模式 前端开发 数据处理
【设计模式】之访问者模式
访问者模式是一种非常有用的设计模式,在前端开发中经常用于处理复杂对象结构和数据集合。它通过将操作和数据结构分离开来,提供了一种优雅而灵活的方式来处理复杂性。通过使用访问者模式,我们可以提高代码的可维护性和可扩展性。然而,在应用访问者模式时需要权衡其带来的优缺点,并根据具体情况进行选择。
34 0
|
5月前
|
设计模式 存储
二十三种设计模式全面解析-揭秘访问者模式:开启对象间灵活交互之门
二十三种设计模式全面解析-揭秘访问者模式:开启对象间灵活交互之门