T
T
topuserman2020-04-22 23:36:39
OOP
topuserman, 2020-04-22 23:36:39

When do we use the Strategy pattern, and when do we use the Decorator pattern?

Do I understand correctly that if I develop some system from scratch, then it is better to use the Strategy pattern (if necessary), and if the system already exists (component), then to expand, you need a Decorator?

Those. the essence of the question is that the Decorator usually applies to the code that has already been written and it is impossible to make changes to the logic of work, and with the help of this pattern new features are added?

Or what are their differences and similarities?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2020-04-22
@topuserman

Strategy = polymorphism, that is, we are tied to a certain interface, and which implementation is not important to us. This is a story about addiction. Well, for example, the postman gives pensions to grandmothers (any one, which grandmother exactly - depends on the strategy, WHICH IS NOT RELATED with the modification of a particular grandmother :)
The decorator is about adding functionality within the framework of one interface, the question of any relationship is not considered at all (for example, a grandmother and a postman), here it is considered - a grandmother in a fur coat or a grandmother with a tan or a grandmother in a stroller, still the same grandmother, but " wrapped" in some kind of behavior :) The main thing is that the grandmother always remains to be the same grandmother for everyone. That is, this is not a confrontation - neither at the beginning nor in the middle we are tied to the additional behavior of the grandmother at the postman. Grandmother and that’s it, and which one depends on the separation strategy (for example, by the name of the street). If a “decorated” grandmother-muscle comes out to him, he will give her a pension as well as not a muscle, because she is just a certain subject / abstraction for him, the main thing is that age and full name agree.
Accordingly, these are not similar patterns in any way, one is behavioral, the other is structural ... They are always applied at any stage of development. At the very beginning, I can make a decorator for an ordinary grandmother in the form of a singing grandmother, and write a postman or grandson in the form of a strategy later. Or I can do the opposite - first teach how to carry grandmothers on a tram (through strategy), and add grandmothers with crutches (decorator) later ...
In some places the analogies are not correct, and in the analogy the strategy is a grandmother, but in general, for understanding the norms and not critical :)

S
Sergey Shvyrev, 2020-04-23
@CellycoMobiles

1. Strategy.
The same application uses the same libraries, but depending on the use, it has different functionality.
Example: ffmpeg and ffplayer - have ~ one source code, when compiled, a separate player is obtained, a separate converter.
2. Decorator.
The behavior of the parent object is enriched without changing the core functionality.
For example : button and switch in html .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question