zoukankan      html  css  js  c++  java
  • 设计模式:访问者模式

    访问者模式(Visitor):表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。

    namespace VisitorDesign
    {
        public abstract class Visitor
        {
            public abstract void VisitConcreteElementA(ConcreteElementA concreteElementA);
            public abstract void VisitConcreteElementB(ConcreteElementB concreteElementB);
        }
        public class ConcreteVisitor1 : Visitor
        {
            public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
            {
                Console.WriteLine("{0}被{1}访问", concreteElementA.GetType().Name, this.GetType().Name);
            }
    
            public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
            {
                Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name);
            }
        }
        public class ConcreteVisitor2 : Visitor
        {
            public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
            {
                Console.WriteLine("{0}被{1}访问", concreteElementA.GetType().Name, this.GetType().Name);
            }
    
            public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
            {
                Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name);
            }
        }
        public abstract class Element
        {
            public abstract void Accept(Visitor visitor);
        }
        public class ConcreteElementA : Element
        {
            public override void Accept(Visitor visitor)
            {
                visitor.VisitConcreteElementA(this);
            }
            public void OperationA()
            {
    
            }
        }
        public class ConcreteElementB : Element
        {
            public override void Accept(Visitor visitor)
            {
                visitor.VisitConcreteElementB(this);
            }
            public void OperationB()
            {
    
            }
        }
        public class ObjectStructure
        {
            private IList<Element> elements = new List<Element>();
            public void Attach(Element element)
            {
                elements.Add(element);
            }
            public void Detach(Element element)
            {
                elements.Remove(element);
            }
            public void Accept(Visitor visitor)
            {
                foreach(Element e in elements)
                {
                    e.Accept(visitor);
                }
            }
        }
    }
    View Code

    测试代码:

                ObjectStructure o = new ObjectStructure();
                o.Attach(new ConcreteElementA());
                o.Attach(new ConcreteElementB());
                ConcreteVisitor1 v1 = new ConcreteVisitor1();
                ConcreteVisitor2 v2 = new ConcreteVisitor2();
                o.Accept(v1);
                o.Accept(v2);
    View Code
  • 相关阅读:
    多继承
    NavigationController的使用
    WebService概述
    IOS block 教程
    多测师肖老师__项目讲解(12.3)
    跨平台跨服务器跨网站SSO(单点登录)方案的DEMO
    使用SQL Server Profiler
    asp.net生成高质量缩略图通用函数(c#代码),支持多种生成方式
    SQL优化实例:从运行30分钟到运行只要30秒
    测试工具的选择和使用
  • 原文地址:https://www.cnblogs.com/uptothesky/p/5287360.html
Copyright © 2011-2022 走看看