zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--访问者模式

     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:29:39 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Element说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public abstract class Element
    12     {
    13         public abstract void Accept(Visitor visitor);
    14     }
    15 }
    View Code
     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:31:19 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteElementA说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteElementA:Element
    12     {
    13         public override void Accept(Visitor visitor)
    14         {
    15             visitor.VisitConcreteElementA(this);
    16         }
    17 
    18         public void OperationA()
    19         { }
    20     }
    21 }
    View Code
     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:31:52 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteElementB说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteElementB:Element
    12     {
    13         public override void Accept(Visitor visitor)
    14         {
    15             visitor.VisitConcreteElementB(this);
    16         }
    17 
    18         public void OperationB()
    19         { }
    20     }
    21 }
    View Code
     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:28:19 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Visitor说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public abstract class Visitor
    12     {
    13         public abstract void VisitConcreteElementA(ConcreteElementA concreteElementA);
    14         public abstract void VisitConcreteElementB(ConcreteElementB concreteElementB);
    15     }
    16 }
    View Code
     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:30:34 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteVisitor1说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteVisitor1:Visitor
    12     {
    13         public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
    14         {
    15             Console.WriteLine("{0}被{1}访问",concreteElementA.GetType().Name ,this.GetType().Name);
    16         }
    17 
    18         public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
    19         {
    20             Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name);
    21         }
    22     }
    23 }
    View Code
     1 using System;
     2 
     3 namespace Visitor
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/6/4 6:34:58 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// ConcreteVisitor2说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class ConcreteVisitor2:Visitor
    12     {
    13         public override void VisitConcreteElementA(ConcreteElementA concreteElementA)
    14         {
    15             Console.WriteLine("{0}被{1}访问", concreteElementA.GetType().Name, this.GetType().Name);
    16         }
    17 
    18         public override void VisitConcreteElementB(ConcreteElementB concreteElementB)
    19         {
    20             Console.WriteLine("{0}被{1}访问", concreteElementB.GetType().Name, this.GetType().Name);
    21         }
    22     }
    23 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 
     4 namespace Visitor
     5 {
     6     /// <summary> 
     7     /// 作者:bzyzhang
     8     /// 时间:2016/6/4 6:38:05 
     9     /// 博客地址:http://www.cnblogs.com/bzyzhang/
    10     /// ObjectStructure说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    11     /// </summary> 
    12     public class ObjectStructure
    13     {
    14         private List<Element> elements = new List<Element>();
    15 
    16         public void Attach(Element element)
    17         {
    18             elements.Add(element);
    19         }
    20 
    21         public void Detach(Element element)
    22         {
    23             elements.Remove(element);
    24         }
    25 
    26         public void Accept(Visitor visitor)
    27         {
    28             foreach (Element element in elements)
    29             {
    30                 element.Accept(visitor);
    31             }
    32         }
    33     }
    34 }
    View Code
     1 namespace Visitor
     2 {
     3     class Program
     4     {
     5         static void Main(string[] args)
     6         {
     7             ObjectStructure objectStructure = new ObjectStructure();
     8 
     9             objectStructure.Attach(new ConcreteElementA());
    10             objectStructure.Attach(new ConcreteElementB());
    11 
    12             ConcreteVisitor1 concreteVisitor1 = new ConcreteVisitor1();
    13             ConcreteVisitor2 concreteVisitor2 = new ConcreteVisitor2();
    14 
    15             objectStructure.Accept(concreteVisitor1);
    16             objectStructure.Accept(concreteVisitor2);
    17         }
    18     }
    19 }
    View Code
  • 相关阅读:
    [Silverlight]App.Current Events中的Startup,UnhandledException以及Exit事件
    [Silverlight]使用DoubleAnimation为对象添加动画效果
    [翻译]ASP.NET MVC Tip #39 – 在ASP.NET MVC中使用分布式缓存
    [翻译]ASP.NET MVC CodePlex Preview 5 更新细节(未完成)
    [Silverlight]打造具有放大缩小效果的图片导航
    [Silverlight]Silverlight2中打造SplashScreen 1
    [Silverlight]如何创建超链接
    [Silverlight]TextBlock控件全攻略
    [转]ajax框架比较
    MonoRail学习之源码放送
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5558054.html
Copyright © 2011-2022 走看看