tag:blogger.com,1999:blog-8712770457197348465.post5748693019446058633..comments2024-03-29T03:17:04.645-07:00Comments on Javarevisited: Strategy Design Pattern in Java using Enum - Tutorial Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8712770457197348465.post-81632668183815396422015-04-22T05:04:40.597-07:002015-04-22T05:04:40.597-07:00Easy to make this compatible with the Open-Close P...Easy to make this compatible with the Open-Close Principle really.<br /><br />Just ensure that the concrete Enum implements an interface; e.g. BattingStrategy; and rename the Enum to e.g. DefaultBattingStrategies<br /><br />Then change the constructor of the Player class and the setStrategy methods to take the BattingStrategy interface.<br /><br />That way, there is no need to modify the Enum Flamming_Pythonhttps://www.blogger.com/profile/15617655721067869066noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77348291025330239472014-12-10T00:14:12.201-08:002014-12-10T00:14:12.201-08:00@Jan nowak, this still voilates open closed princi...@Jan nowak, this still voilates open closed principle, which is the main aim of using Strategy pattern. Though its good solution in some cases but we cannot call it strategy pattern.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11274508495688178192014-11-29T14:41:11.262-08:002014-11-29T14:41:11.262-08:00@gusAbove: The only problem are enums residing in ...@gusAbove: The only problem are enums residing in a single compilation unit. You edit this file, true, but no need of messing in other enum instances. Lets be honest: have you ever added new class to the project code without recompiling whole module/project? Player class doesn't change -and this is what's most important - from the client's perspective nothing changes here. Besides, Anonymoushttps://www.blogger.com/profile/17949315112183280480noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36320079816195136872014-11-21T19:50:36.870-08:002014-11-21T19:50:36.870-08:00@Anonymous, you are right, it does violate the ope...@Anonymous, you are right, it does violate the open closed design principle, because if you would like to add a new strategy let's say for 10 over match, you have to modify existing class i.e. the enum itself. So in true sense it's not really the GOF Strategy pattern, which allowed you to add new strategy without modifying existing, already tested class. This is specifically good on casesjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-64029551933241530782014-11-21T07:16:53.075-08:002014-11-21T07:16:53.075-08:00Don't you think with this approach we are goin...Don't you think with this approach we are going back to if-else way. The whole purpose of Strategy pattern is to not to touch existing class and add new class (Open for extension closed for modification). With this approach we are essentially back to modifying the same class, each time I need to add extra behaviour. Anonymousnoreply@blogger.com