标签:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
访问者模式
*/
namespace App_MYCS.HDL_SJMS.FWZMS
{
class my_FWZMS
{
public void dy()
{
objStructure o = new objStructure();
o.Attach(new conElementa());
o.Attach(new conElementb());
ConVisitor1 cv1 = new ConVisitor1();
ConVisitor2 cv2 = new ConVisitor2();
o.Accept(cv1);
o.Accept(cv2);
}
}
abstract class Visitor
{
public abstract void Visitor1(conElementa a);
public abstract void Visitor2(conElementb b);
}
class ConVisitor1 : Visitor
{
public override void Visitor1(conElementa a)
{
Console.WriteLine("conElementa 被 ConVisitor1 访问 ");
}
public override void Visitor2(conElementb b)
{
Console.WriteLine("conElementb 被 ConVisitor1 访问 ");
}
}
class ConVisitor2 : Visitor
{
public override void Visitor1(conElementa a)
{
Console.WriteLine("conElementa 被 ConVisitor2 访问 ");
}
public override void Visitor2(conElementb b)
{
Console.WriteLine("conElementb 被 ConVisitor2 访问 ");
}
}
abstract class Element
{
public abstract void Accept(Visitor vi);
}
class conElementa : Element
{
public override void Accept(Visitor vi)
{
vi.Visitor1(this);
}
//其它相关方法
public void a()
{ }
}
class conElementb : Element
{
public override void Accept(Visitor vi)
{
vi.Visitor2(this);
}
//其它相关方法
public void b()
{
}
}
class objStructure
{
private List<Element> el = new List<Element>();
public void Attach(Element e)
{
el.Add(e);
}
public void Detach(Element e)
{
el.Remove(e);
}
public void Accept(Visitor v)
{
foreach (Element item in el)
{
item.Accept(v);
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/longkai178/p/5815101.html