Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • 6
    Even when you do end up with multiple uses of the enum, there's still a relatively straightforward fix to that: Use the "real" (OOP) strategy pattern, and write a function (method) that converts your enum values into strategy objects. Then, for each place where you would have interpreted the enum values, you instead make a polymorphic call into the strategy object. The language enforces that every abstract method must be overridden in all concrete subclasses, so you cannot "forget" one of the methods. In this way, the enum still exists, but you only interpret it once. Commented Oct 8, 2021 at 21:14
  • 23
    This is not a Strategy pattern In a proper strategy pattern, the algorithm is contained outside the dispatcher logic, and passed in, typically using an interface. Allowing near infinite possibilities to extend the code. Here the algorithm is inside existing code, and the enum just chooses. There is no way to extend, for the outside to provide a whole new algorithm. E.g. see tutorialspoint.com/design_pattern/strategy_pattern.htm Commented Oct 9, 2021 at 15:56
  • 3
    Not sure why this answer is so highly upvoted. This answer focuses on how it might, technically, fit the definition of a strategy pattern in a none-OOP setting (and, as @user949300 said, that is debatable). It ignores that in an OOP setting it is definitely an anti-pattern, and should be tackled in some other way. Commented Oct 10, 2021 at 12:41
  • 2
    @Omegastick, I'd suggest it's highly upvoted because most people think you mistaken in your view that it is an anti-pattern. Commented Oct 10, 2021 at 12:53
  • 2
    It is not a strategy pattern if you just invoke a function statically in the code, but it can be a strategy pattern if the function can be chosen dynamically, for that you would need to resort to e.g. high-order programming. Commented Oct 10, 2021 at 14:56