Follow the OCP principle or use "instanceof"

Asked

Viewed 66 times

4

The OCP principle preaches: "open to extension, but closed to alteration". To achieve this we need to abstract, because with an abstraction we can extend without needing to change the one that uses abstraction.

Consider a didactic example:

ocp

The idea is that I can extend the project by adding new letters without, however, needing to change the class Palavra.

So far, so good: abstraction, polymorphism, OCP.

But if I need to know the amount of certain letters, the amount of letter A, B, etc.?

One of the ways to do this is by using instanceof. But that way, I’ll be violating OCP, because with each addition of a letter, I’ll have to change the Word class to add one more if and instanceof to count the amount of that letter.

Finally, the questions are:

  • there is a way to avoid this?
  • the need to know specifically about an object misrepresents the adoption of this abstraction, that is, since I need to know about the object, I do not have to abstract this object?
  • abstraction use in one sense, but inevitably concrete, specific (contrary to abstract) in another direction?
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

  • I think the Interface Segregation Principle (ISP) can help you a little bit.

1 answer

2

Has some solutions.

  1. One of them is to violate the OCP. Who said that it is inviolable? There is a reason for it to exist, but it is not to follow it at any cost. This is usually the simplest solution and solves well in almost every case. If you need the class to know its derivatives for some reason, do it. Avoid, see if you can’t do otherwise, but can do if you understand the consequences.

    Modern languages have created mechanisms of their own that violate many of the rules they’ve created, why do you think they’re doing this? Do you think they’re making a mistake, or do you think after a while they consider these rules to be less important? Of course everything has to be analyzed, it does not mean that the rule should never be used.

  2. Another is to create a derived class registration system. It can be done even in the base class. When it does this it creates a dynamism in the code, so instead of having to change class it changes the data at runtime, so it doesn’t need to mess with the class when there is a new derivative.

    To do this you need a mechanism, probably with a dictionary in the base class and something that allows you to register the class in that dictionary, then the base class is aware of the derived classes at runtime and can consult them as you wish.

    Probably the base class has at least one method that is already the registration mechanism of the derived class in this dictionary and all derivatives will have it and if you do it right you end up forcing the record always.

    I would not do this in almost any case, I do not like to go against the philosophy of language to follow a rule, with the detail that this mechanism can cause more problems. I see that a lot. If the language is dynamic it is not so bad, but I find a complex solution to solve a pro9belma that does not exist if it does not follow a rule that in general the person does not know why is doing it.

  3. I will not talk about all solutions, but another is to use reflection, if available, to identify all classes that derive from this base in every application and to be able to count as you wish. Again I find a bad solution to most cases. If it is to insist a lot on this I would opt for a code generator that does the same as reflection, but at compile time, leaving faster and robust, solving the OCP without having anything additional, but I admit that it is not simple to do for most people.

  • I would solve it the first way, I actually did something similar to store the visual components of a Swing application and tweak the size of your fonts evenly. I used WeakReference in the key-value mapping and until that worked well. It’s just not automatic, meaning each new component tree needs to be manually added to the map, this is subject to errors. If it were automatic it would be a much better solution.

  • Ah, the removal is also manual (I mean, programmatic).

  • I meant the second way

Browser other questions tagged

You are not signed in. Login or sign up in order to post.