• C#使用反射特性构建访问者模式


    代码出自《c#3.0设计模式》

    两个结构的对象

        class Element
        {
            public Element Next { get; set; }
            public Element Part { get; set; }
            public Element() { }
            public Element(Element next)
            {
                Next = next;
            }
        }
        class ElementWithLink : Element
        {
            public ElementWithLink(Element part, Element next)
            {
                Next = next;
                Part = part;
            }
        }



    与反射相关的核心代码

        abstract class IVisitor
        {
            public void ReflectiveVisit(Element element)
            {
                Type[] types = new Type[]{element.GetType()};
                //搜索参数与指定参数类型匹配的指定公共方法
                //第一个参数是方法名称
                //第二个参数为参数对象的数组,顺序和类型必须一致
                MethodInfo methodinfo = this.GetType().GetMethod("Visit", types);
                if (methodinfo != null)
                {
                    //使用指定的参数调用当前实例所表示的方法或构造函数
                    //第一个参数是被调用方法的对象的实例
                    //第二个参数是该方法的参数,顺序和类型都必须一致
                    methodinfo.Invoke(this, new object[] { element });
                }
                else
                {
                    Console.WriteLine("Unexpected Visit");
                }
            }
        }


    如果不懂请看注释

    访问器

        class CountVisitor : IVisitor
        {
            public int Count { get; set; }
            public void CountElements(Element element)
            {
                ReflectiveVisit(element);
                if (element.Part != null)
                {
                    CountElements(element.Part);
                }
                if (element.Next != null)
                {
                    CountElements(element.Next);
                }
            }
            public void Visit(ElementWithLink element)
            {
                Console.WriteLine("not counting");
            }
            public void Visit(Element element)
            {
                Count++;
            }
        }



    客户端代码

        class Program
        {
            static void Main(string[] args)
            {
                Element objectStructure = new Element(
                                            new Element(
                                                new ElementWithLink(
                                                    new Element(
                                                        new Element(
                                                            new ElementWithLink(
                                                                new Element(null),new Element(null)
                                                            ))),
                                                            new Element(
                                                                new Element(
                                                                    new Element(null)
                                                                    )))));
                Console.WriteLine("count it");
                CountVisitor visitor = new CountVisitor();
                visitor.CountElements(objectStructure);
                Console.WriteLine(visitor.Count);
                Console.ReadKey();
            }
        }


    关于访问者模式    反射的内容
    以后肯定要更详细的介绍

  • 相关阅读:
    CF D. Ehab and the Expected XOR Problem 贪心+位运算
    luogu 1903 [国家集训队]数颜色 / 维护队列 带修改莫队
    Test 1 T2 B 线段树合并
    CF812C Sagheer and Nubian Market 二分+贪心
    CF804B Minimum number of steps
    CF796D Police Stations BFS+染色
    CF796C Bank Hacking 细节
    k8s节点NotReady问题处理
    Elastic-Job快速入门
    Elastic-Job介绍
  • 原文地址:https://www.cnblogs.com/liulun/p/1538875.html
Copyright © 2020-2023  润新知