Inheritance or identifier

Anyone have opinions on when the user is inherited and when to use the identifier?

Inheritance example:

class Animal { public int Name { get; set; } } class Dog : Animal {} class Cat : Animal {} 

ID example:

 class Animal { public int Name { get; set; } public AnimalType { get; set; } } 

In what situations should I choose which solution and what are the pros and cons for them?

/ Lina

+6
java inheritance c #
source share
10 answers

It depends on the type of actions that need to be performed on your system. For example, if you have a common action that must be performed differently for each of your objects than using inheritance, for example:

 class Shape { public abstract void Draw(); } class Circle : Shape { public override void Draw() { /* Custom implementation for Circle. */ } } class Box : Shape { public override void Draw() { /* Custom implementation for Box. */ } } 

On the other hand, if some type and object are more like its property, you should use what you called an "identifier":

 enum Color { Red, Green, Blue } class Box { public Color BoxColor { get; set; } } 

You can use inheritance here (but it doesn't look so good): RedBox, BlueBox, etc. The difference is that all fields (even those with different colors) will be processed the same way, and their behavior will be common.

+6
source share

What you call an โ€œidentifier,โ€ I would call an enum approach.

Using renaming for sub-division is only suitable for highlighting a small fixed set of features. When you expand it, you will see statement statements appearing everywhere in your code, and this is a strong indicator that you should use inheritance instead.

+4
source share
  • If the only difference between animals is their type, use the AnimalType property.

  • If different animals have different implementations of properties and methods or additional, create classes that come from Animal .

    If animal type checking is a common use case, consider providing an AnimalType property, and also as an alternative to the is / as .

+3
source share

I would use inheritance if the inheriting classes (Cat and Dog in your example) have a different interface (methods or fields) or if they are used to select different behavior. For example, in a real project, I have a base class for generating HTML code. Depending on the situation, I create the correct subclass that generates the correct HTML. These classes have (basically) the same interface, but their behavior is so different that I do not want to select it with if or switch.

+2
source share

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() { // Wag } } class Noise { DoYourThing() } class Bark : Noise { DoYourThing() { // Bark } } class Talk : Noise { DoYourThing() { // Talk } } class Animal { public Noise { get; set; } public Tail { get; set; } } 

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.

+2
source share

See where you use the code and how you use it. I find that inheritance always pays off when you have different objects that are well placed for polymorphism.

Think of a solution for logging, the log log(string info) method should be inherited for an object that is different from what it does, for example, to enter the disk, to enter the network, screen, etc.)

I found that the smell of code rises when you need to go back to the real class in order to do something. If you do this, you probably have not used it appropriately.

Inheritance is just a tool, donโ€™t use it because you can use it because it will help you write clearer code that works more efficiently.

+1
source share

Inheritance, in my opinion, is preferable when the behavior should change in accordance with this identifier. As an example, if you just want to provide your Animal a walk() method, you don't need to provide subclasses (since dogs and cats seem to go the same way from afar).

However, if you want them to have some makeNoise(Mood mood) method, inheritance would be useful:

 class Cat { public void makeNoise(Mood mood) { swithch(mood) { case happy: purr(); break; // and so on 
+1
source share

Moving from general to specialized, I would say go with inheritance. Thus (in relation to your example) you will only need to do a walk, eat, sleep, etc. once. If they are on the same level, it is better to use an identifier. For example. You would not want to rely on a cat dog, because then you will have to disable options in addition to adding new ones.

+1
source share

This is unsuccessful, two come to solve two different problems. If you have different behaviors , you should use Inheritance, for example, if the animal class has a "talk" behavior, it should be implemented using polymorphism and inheritance.

0
source share

Well, my new rule of thumb will be to inherit if the classes have different types of behavior, otherwise I will go for some kind of identifier. (Of course, the final decision will depend on the environment in which the classes will be used). Thanks for all your answers!

0
source share

All Articles