G
G
gitdev2020-09-13 19:04:35
OOP
gitdev, 2020-09-13 19:04:35

Can you give an example of the open/closed principle?

Can you give an example of the open/closed principle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-09-13
@gitdev

Decorator - decorating behavior is moved to another class with another name within the same abstraction (the interface is respected). This is respected by the open closed principle.
But if Decorating behavior is placed in the original class itself with if / else, this is a violation of the principle.
Example: you have a grandmother, you taught the pension system to work with this grandmother. But your app needs a grandmother dressed in protective gear, wearing a COVID-19 mask, and able to ski. If all this behavior is stuffed into the grandmother, you will violate the principle, because you will have to edit the grandmother, or you can make the decorators "grandmother with skis" and "grandmother with a mask" and all this will be the same correct grandmother, who can easily make a new change with a new decorator , and don't shake all the old behavior.
The decorator is not the only correct way to comply, but it illustrates well, for example, it can be a great way to isolate some behavior into a separate abstraction

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question