本文整理汇总了C#中Cat.Accept方法的典型用法代码示例。如果您正苦于以下问题:C# Cat.Accept方法的具体用法?C# Cat.Accept怎么用?C# Cat.Accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cat
的用法示例。
在下文中一共展示了Cat.Accept方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Cat c = new Cat();
Dog d = new Dog();
Shiba s = new Shiba();
Mutant m = new Mutant();
var visitor = new MakeSoundVisitor();
// Classic visitor pattern allows us to add behavior to existing objects without modifying them
Console.WriteLine("Call Accept() using classic visitor");
c.Accept(visitor);
d.Accept(visitor);
s.Accept(visitor);
m.Accept(visitor);
// Think of the Accept() method as a way to delegate the desired action to whichever Visitor implementation is passed in
// Virtual functions are dispatched dynamically while function overloading is done statically.
// To get around this, double dispatch is used
Console.WriteLine("Testing wrapper");
var wrappedCat = new AnimalWrapper(c);
wrappedCat.Accept(visitor);
var dynamicVisitor = new MakeSoundDynamicVisitor();
Console.WriteLine("Call Accept() using dynamic visitor");
c.Accept(dynamicVisitor);
d.Accept(dynamicVisitor);
s.Accept(dynamicVisitor);
m.Accept(dynamicVisitor);
Console.WriteLine("Direct call to dynamic visitor");
dynamicVisitor.Visit(c);
dynamicVisitor.Visit(d);
dynamicVisitor.Visit(s);
dynamicVisitor.Visit(m);
Console.ReadKey();
}