Delete comment from: Javarevisited
Easy to make this compatible with the Open-Close Principle really.
Just ensure that the concrete Enum implements an interface; e.g. BattingStrategy; and rename the Enum to e.g. DefaultBattingStrategies
Then change the constructor of the Player class and the setStrategy methods to take the BattingStrategy interface.
That way, there is no need to modify the Enum code at all when a new batting strategy implementation is warranted; the client can simply supply a new BattingStrategy implementation of their own, or a new Enum class which extends BattingStrategy can be created in the same package by the original developer, e.g. ExtraBattingStrategies, and with the same access permissions as DefaultBattingStrategies
Apr 22, 2015, 8:04:40 AM
Posted to Strategy Design Pattern in Java using Enum - Tutorial Example

