C#C
C#3y ago
13 replies
LazyGuard

❔ Refactoring strategy pattern to a simpler code

I have the following code and I want te refactor it to a less-boiler-plate code.

Wha'ts suposed to do is that the Handle method should take a product as input and
1.decides to which output system it needs to send information (depending on some configuration and on the product itself). There could be more thant 1 output systems to send the product to.
2. convert the Product to the appropriate format for chosen output systems.
3. send to those systems


public class XMLHandler
{
    private readonly IOutputSystemStrategyFactory _strategyFactory;
    private readonly IOutputSystemDecider _outputSystemsDecider;

    public XMLHandler(IOutputSystemStrategyFactory strategyFactory, IOutputSystemDecider outputSystemsDecider)
    {
        _strategyFactory = strategyFactory;
        _outputSystemsDecider = outputSystemsDecider;
    }

    public void Handle(Product product)
    {
        IEnumerable<OutputSystem> outputSystems = _outputSystemsDecider.GetOutputSystems(product);

        foreach (OutputSystem outputSystem in outputSystems)
        {
            IOutputSystemStrategy strategy = _strategyFactory.Create(outputSystem);
            strategy.GenerateAndSend(product);
        }
    }
}
Was this page helpful?