0

I have two choices to create the "ExpectedHeader", through a factory or via the strategy pattern. Maybe I should add, that a strategy-object already exists (used to customize the ExpectedModel to the TestParameters). I only would have to extend by the method #createExpectedHeader. Is there any obvious advantage of one over the other?

1. Through a factory:

public class ModelBuilder {

    //uses factory
    ExpectedHeaderFactory factory;

    public ExpectedModel deriveExpectedModel(TestParameters testParams) {
        ExpectedHeader expectedHeader = factory.createExpectedHeader(testParams);
        return ExpectedModel.builder()
                    .setExpectedHeader(expectedHeader)
                    .build();
    }

}

2. through a strategy pattern:

public class ModelBuilder {

    //uses injected strategy
    IStrategy strategy;

    public ExpectedModel deriveExpectedModel(TestParameters testParams) {
        ExpectedHeader expectedHeader = strategy.createExpectedHeader();
        return ExpectedModel.builder()
                    .setExpectedHeader(expectedHeader)
                    .build();
    }

}

The strategy interface (the first two methods are already used in other methods of the ModelBuilder)

public Interface IStrategy {

     public modifyText();
     public isRelevant();
     public createExpectedHeader();
}
cobby
  • 484
  • 3
  • 18
  • 4
    Factory is a creational pattern, strategy is a behavioural pattern. Need your objects to change behaviour on the fly? Use a strategy. Your first example is a factory pattern, your second example is a factory pattern with misleading naming. – Kayaman Apr 05 '22 at 13:18
  • This might help [Strategy Design Pattern and Factory Method Design Pattern](https://stackoverflow.com/questions/5375187/strategy-design-pattern-and-factory-method-design-pattern) – Melchizedek Apr 05 '22 at 13:19
  • @Kayaman well, is createExpectedHeader() not also a behaviour; a different behaviour of how an object is created? Further, using the strategy pattern I would collect all customization within one class. As said, I already use a strategy within the ModelBuilder, that customizes the ExpectedModel based on the test parameters. – cobby Apr 05 '22 at 13:22
  • 2
    Strategy is not [classified](https://en.wikipedia.org/wiki/Strategy_pattern) as a creational pattern, but a behavioural one like I already said. You can design what you want, but they're probably not going to fit the GoF definitions. – Kayaman Apr 05 '22 at 13:32
  • I second @Kayaman's second comment: there is no GoF design pattern here. The examples seem to be based simply on naming conventions... naming a method "create" does not make it a design pattern, nor does naming an interface "Strategy". – jaco0646 Apr 06 '22 at 00:00
  • [`RawNewsProvider`](https://stackoverflow.com/q/72909148/230513) is an example where _strategy pattern_ might apply: The interface defines methods common to news items, while disparate implementations hide details of how items are acquired. – trashgod Nov 13 '22 at 23:36

0 Answers0