DesignPattern_Behavioral_Visitor

void Main()
{
    (new Director()).Show(new VisitorA());
}

class Director{
    List<Element> elements = new List<Element>{
        new ElementA(),
        new ElementB()
    };
    public void Show(Visitor v){
        foreach (var element in elements)
        {
            element.Show(v);
        }
    }
}
abstract class Element
{
    public abstract void Show(Visitor v);
}
class ElementA:Element{
    public override void Show(Visitor v){ v.ByEleA(this); }
}
class ElementB:Element{
    public override void Show(Visitor v){ v.ByEleB(this); }
}
abstract class Visitor{
    public abstract void ByEleA(ElementA ea);
    public abstract void ByEleB(ElementB eb);
}
class VisitorA:Visitor{
    public override void ByEleA(ElementA ea){
        "EA meet VA".Dump();
    }
    public override void ByEleB(ElementB eb){
        "EB meet VA".Dump();
    }
}
class VisitorB:Visitor{
    public override void ByEleA(ElementA ea){
        "EA meet VB".Dump();
    }
    public override void ByEleB(ElementB eb){
        "EB meet VB".Dump();
    }
}
时间: 2024-11-07 07:40:53

DesignPattern_Behavioral_Visitor的相关文章