Home > Back-end >  How to avoid duplicate code while using Strategy Design Pattern?
How to avoid duplicate code while using Strategy Design Pattern?

Time:01-15

I am new to design patterns and thinking about using Strategy design pattern for implementing code in my backend service. However, the Strategies are having duplicate code. I have the following classes:-

class StrategyA implements Strategy {
  private Helperclass1 helperclass1;
  private Helperclass2 helperclass2;
  private Daoclass dao;

  public void execute(Object obj) {
    updatedObj = helperclass1.method(obj);
    updatedObj = helperclass2.method2(updatedObj);
    updatedObj = updateObj(updatedObj);
    dao.update(updatedObj);
  }

  private Object updateObj(Object obj) {
    //update obj & return;
  }
}

class StrategyB implements Strategy {
  private Helperclass1 helperclass1;
  private Helperclass2 helperclass2;
  private Daoclass dao;

  public void execute(Object obj) {
    updatedObj = helperclass1.method(obj);
    updatedObj = helperclass2.method2(updatedObj);
    dao.update(updatedObj);
  }
}

class StrategyC implements Strategy {
  private Helperclass1 helperclass1;
  private Daoclass dao;

  public void execute(Object obj) {
    updatedObj = helperclass1.method(obj);
    dao.update(updatedObj);
  }
}

What should I do to remove duplicate code from Strategy pattern? I am considering not using the design pattern to avoid code duplication. Can anyone suggest a better design pattern for this usecase? I read about some similar situations and found that Command or Template patterns may be considered as an alternative (link:What pattern to use with a Strategy Pattern to avoid duplicate code inside Concrete Strategies?). However I am not sure how I can effectively use these patterns for my use-case.

CodePudding user response:

You can make one general strategy that executes multiple strategies. Like event handling. The idea:

interface Strategy
{
    Object execute(Object obj);
}
class MultiStrategy implements Strategy
{
    private Strategy[] strategies;
    public Object execute(Object obj)
    {
        foreach (Strategy s : strategies)
            obj = s.execute(obj);
        return obj;
    }
}
class Strategy1 implements Strategy
{
    private Helperclass1 helperclass1;
    public Object execute(Object obj)
    {
        return helperclass1.method1(obj);
    }
}
class Strategy2 implements Strategy
{
    private Helperclass1 helperclass2;
    public Object execute(Object obj)
    {
        return helperclass2.method2(obj);
    }
}

And the usage would be kinda like:

Strategy s = new MultiStrategy(new Strategy[]
{
    new Strategy1(helper1),
    new Strategy2(helper2),
});

In my example the strategies 1 and 2 and in fact adapters, but that's also a common use case. The benefit of this is that you can use the same code of MultiStrategy for any number of substeps, or even combine multiple multi strategies under the Strategy interface to make trees of strategies. Note though, that this MultiStrategy is another pattern in and of itself, just using a single-layer strategy is not enough to clean this up in your case. I don't remember they call it officially, but I guess you can call it event handling.

CodePudding user response:

You can eliminate duplicate code through inheritance, if you can factor the common code out.

  • Related