Campbell Ritchie wrote:
It already exists in the java.lang package, so you should think of a different name for it. Maybe Warrior.vishnu priyag wrote:. . . Character is a class . . .
Call your implementations Sword and Shield rather than XXXWeapon.Are you deciding the weapon at runtime, or at compile time or before? If you can make a Warrior change between a Sword and a Shield at runtime, that would be something different. This Wikipedia page says the Strategy Pattern should permit changes of the algorithm at runtime. You have a plain simple field there, and I don't see any code to change it.In runtime, with the help of constructor, we can pass any weapon impl to the Weapon instance variable of the Character class.
Zachary Griggs wrote:Lots of good solutions here!
Personally I made the method static because if I had to use this in my project, I would create some sort of StringHelper or StringUtility class that does not get instantiated. The method itself doesn't need any sort of state to operate correctly. Though this is very much open for debate; I don't think there's a "correct" answer here so I'd be interested in hearing other perspectives.
I believe you could also do a toSet() call, rather than the .distinct().toList(). Though that works completely fine as well.