这个设计使用动态还好吗?

Dan*_*ars 4 c# polymorphism design-patterns dynamic dispatch

我在应用程序中有一个要处理的不同作业列表.我正在使用一种使用不同类型来表示不同类型作业的设计 - 这很自然,因为它们具有不同的属性等等.对于处理,我正在考虑根据下面的代码在C#中使用dynamic关键字.

abstract class Animal {}    
class Cat : Animal {} 
class Dog : Animal {}

class AnimalProcessor
{
    public void Process(Cat cat)
    {
        System.Diagnostics.Debug.WriteLine("Do Cat thing");
    }

    public void Process(Dog dog)
    {
        System.Diagnostics.Debug.WriteLine("Do Dog thing");
    }

    public void Process(Animal animal)
    {
        throw new NotSupportedException(String.Format("'{0}' is type '{1}' which isn't supported.",
            animal,
            animal.GetType()));
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        List<Animal> animals = new List<Animal>
        {
            new Cat(),
            new Cat(),
            new Dog(),
            new Cat()
        };

        AnimalProcessor animalProcessor = new AnimalProcessor();
        foreach (dynamic animal in animals)
        {
            animalProcessor.Process(animal);
        }

        //Do stuff all Animals need.
    }
}
Run Code Online (Sandbox Code Playgroud)

代码按预期工作,但是,我有一种唠叨的感觉,我错过了一些非常明显的东西,并且有一个更好(或更好知道)的模式来做到这一点.

有没有更好或同样好但更被接受的模式来处理我的动物?或者,这样好吗?并且,请解释为什么任何替代品更好.

das*_*ght 8

你没有遗漏任何东西:调度对象的运行时类型是dynamic可以做得很好的事情.

您确实有其他选择,例如实现访问者模式,但它在实现方面要高得多,并且它的可读性也不如您使用的方法那么可读dynamic:

interface IAnimalProcessor { // The visitor interface
    void Process(Cat cat);
    void Process(Dog dog);
    void Process(Animal animal);
}
class AnimalProcessor : IAnimalProcessor {
    ...
}
interface IProcessable {
    void Accept(IAnimalProcessor proc);
}
class Cat : IProcessable {
    public void Accept(IAnimalProcessor proc) {
        proc.Process(this); // Calls the Cat overload
    }
}
class Dog : IProcessable {
    public void Accept(IAnimalProcessor proc) {
        proc.Process(this); // Calls the Dog overload
    }
}
...
AnimalProcessor animalProcessor = new AnimalProcessor();
foreach (IProcessable animal in animals) {
    animal.Accept(animalProcessor); // The animal will call back the right method of the proc
}
Run Code Online (Sandbox Code Playgroud)