Inheritance will closely link cat and dog behavior with animal behavior. This may be what you want.
However, if you are trying to create a universe where everything is possible, and the animal can change its type, you may prefer to use the identifier approach.
But you can take it further.
According to Fours gang design templates, you should
The "favorable" composition of the object "over" class inheritance ".
An animal is only a dog, because it has a tail that rattles and barks. In your universe, where over time the dog can learn to speak, you will want to change this behavior.
Then you can try to ignore your behavior, and then you can use inheritance as part of this behavior.
With this class structure:
class Tail { DoYourThing() } class WaggyTail : Tail { DoYourThing() {
You can customize your cats and dogs:
Animal dog = new Animal { Noise = new Bark(), tail = new DoggyTail() } Animal cat = new Animal{ Noise = new Purr(), tail = new CattyTail() }
.. then when you need your super breed, you can just change your behavior.
dog.Noise = new Talk ();
.. Hey presto, your dog can talk now .. If you need your dog to sing, you just create a new Sing class .. no further changes are needed.
Mongus pong
source share