Alternative template for strategy

I have a piece of code where I started applying a strategy template, let's say this:

IStrategy
StrategyA : IStrategy
StrategyB : IStrategy
StrategyC : IStrategy

The interface has only the Calculate method. After implementation, it turned out that all 3 specific types have the same code for the Calculate method and two identically named properties, only with different values.

So, to remove duplication, I made the interface an abstract class and moved the method and properties before, just setting the base properties with their corresponding values ​​from the inside of the construction of specific types.

Now I know that templates are not hard and fast rules, but only recommendations, but I have distorted it so far from recommendations that I cannot help but think that there is another template that I should look at?

Can anyone suggest any other approaches, please leave me so that it is easy to add new “Strategies” along the line. It may turn out that we need to change the logic in some of these new cases, since I could structure it so that I don't have code repetition, but I have a flexible design that allows me to change things in a line?

Thank.

+3
source share
4 answers

Another alternative you can use is Template . But he has a problem: he allows you to change the algorithm for new cases, but in a very limited way.

, - , - , , .

0

abstract class BaseStrategy, , ?

+5

paterns, -, Builder, , patern, algorythm .

, , , .

0

Have an IStrategy interface. An abstract BaseStrategy class that implements IStrategy. Thus, you can extend or implement the interface based on the presence or absence of common code, and the client can refer to the interface

0
source

Source: https://habr.com/ru/post/1793808/


All Articles