What does the context class do for the Strategy design standard?

Asked

Viewed 39 times

-1

If Strategy uses its abstract class as a means of instantiating concrete implementations, what is the function of the context? In certain materials it was cited that this patter design would be implemented in a situation like the following:

if $banco == "Santander":
   faca algo
else if $banco == "Itau":
   faca outra coisa
  ....

1 answer

0


What is defined as contexto in this pattern is just a generic way of calling the class that you will use the strategy. It’s where you need a strategy and possibly the choice which one to use.

For example, if you have a class Impostos it can be the context to use strategies according to the state where would have implementations of strategies something like ImpostoSP, ImpostoRJ, etc. In this class it uses an object of one of these types of strategy (possibly more than one if appropriate). These classes treat taxes in general but the detail varies in each state and it consumes this detail of strategies.

Just be careful to take bad examples and learn wrong. The example of the code seems to actually use the pattern, but maybe not in the most appropriate way, but I’m just speculating, I don’t have enough information to state.

More information on How to instantiate Strategy objects.

Browser other questions tagged

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