Currently I am confronted often with code that follows the pattern demonstrated by the code bellow. It is kind of a strategy pattern.
I am not fine with this, it feels somehow smelly. It breaks the actual strategy pattern and the additional indirection is confusing. Also it leads often to methods called similiar like in the example, because the purpose of the method in the derived class is very similar to the in the base class. On the other side I am not able to point with the finger to the problem core.
Am I the onlyone who finds this fishy? And if not, what problems can this code cause, esepecially with regard to SOLID principles?
namespace MyTest
{
abstract class Base
{
public void DoSomething()
{
var param = Prepare();
DoItReally(param);
}
private string Prepare()
{
return "Hallo Welt";
}
protected abstract void DoItReally(string param);
}
class DerivedOne : Base
{
protected override void DoItReally(string param)
{
Console.WriteLine(param);
}
}
class DerivedTwo : Base
{
protected override void DoItReally(string param)
{
Console.WriteLine(param.ToUpper());
}
}
static class Program
{
public static void Main(string[] args)
{
Base strategy = new DerivedOne();
strategy.DoSomething();
strategy = new DerivedTwo();
strategy.DoSomething();
}
}
}
Aucun commentaire:
Enregistrer un commentaire