码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式学习笔记--访问者模式

时间:2016-06-04 07:04:04      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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

 

设计模式学习笔记--访问者模式

标签:

原文地址:http://www.cnblogs.com/bzyzhang/p/5558054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!