c# - Is this design using dynamic okay? -
In order to process my application, I got a list of different jobs. I am working with a design that uses different types to represent different types of jobs - it is natural because they have different qualities and so on. For processing, I was thinking of using dynamic keywords in C # according to the following code:
abstract class animal {} class category: animal {} class dog: animal {} class Animalprocessor {public zero process (cat cat) {System.Diagnostics.Debug.WriteLine (""); } Public Zero Process (Dog Dog) {System.Diagnostics.Debug.WriteLine ("Dog Dog"); } Public Zero Process (Animal Animals) {New NotSupportedException (String.Format ("'{0}' is type '{1}' that is not supported.", Animals, Animals. GetType ())); }} Internal class program {Private Static Zero Main (string [] Args) {List & gt; Animals & gt; Animals = new list & amp; Animal; Animals & gt; {New cat (), new cat (), new dog (), new cat ()}; Animal Processor Animals Processor = New Animal Processor (); Pioneer (dynamic animal in animals) {animalprocessor Process (animal); } // All goods are needed. The code works as expected, but, I am a harasser that I have been clearly remembered and there is a better (or better known) pattern to do this. Is there a better or equally good but more accepted pattern to use for action on animals? Or, is this okay? And, please explain why any option is better. You are not missing anything: The sending object on runtime type is something that is dynamic
can do quite well. You have the option, like implementing it, but in the case of implementation it is very expensive, and it is also not nearly as readable as your code which is dynamic
Uses:
Interface IAnimalProcessor {// Visitor Interface Without Process (Cat Cat); Zero process (dog dog); Zero process (cattle animal); } Class Animal Processor: IAnimalProcessor {...} Interface Interface {IAnimalProcessor Proc;) } Category Cat: IPCasebel {public accepts the public (IAnimalProcessor proc) {proc.Process (this); // call overload call}} square dog: IPCasebel {public zero acceptance (ienimal processor proc) {proc.Process (this); // call dog overload}} ... animalprosessor animal processor = new animalproser (); Agrarian (transitive animal in animals) (Animals. Accept (animalprocessor); // Animals will call right method of proc
Comments
Post a Comment