Hello guys, how are you lot doing? I promise you lot all are fine in addition to doing skilful inwards your life in addition to career. Today, I am going to verbalize nigh the Strategy pattern, i of the useful pattern in addition to coding pattern which volition assist you lot to write flexible code. The code which tin forcefulness out withstand the evidence of fourth dimension inwards Production. I'll too learn you lot how you lot tin forcefulness out utilization Enum to implement the Strategy pattern pattern in addition to Open Closed pattern regulation amend inwards Java. I conduct maintain said this before that Java Enum is really versatile in addition to tin forcefulness out practice lot to a greater extent than than you lot unremarkably await from it. We conduct maintain seen a lot of examples of Enum inwards my before posts like writing thread-safe Singleton using Enum in addition to 10 ways to utilization Enum inwards Java.
In this article, nosotros volition larn a novel way to utilization Enum, for implementing the Strategy pattern pattern. Strategy pattern is i of the famous pattern, which takes payoff of polymorphism, to withdraw switch cases in addition to strive for the open-closed pattern principle.
Formally it encapsulates related algorithm, known equally strategy in addition to brand them interchangeable. So your Client, too known equally Context, tin forcefulness out utilization a dissimilar algorithm or strategy, without whatsoever modification.
One of the substitution advantages of Strategy pattern is it's extensibility, similar introducing a novel Strategy is equally slow equally writing a novel cast in addition to implementing Strategy interface, alongside Enum, instead of creating a split class, you lot practice a split Enum instance, which agency less publish of classes in addition to sum practice goodness if Strategy pattern.
Btw, if you lot are novel into the basis of pattern pattern in addition to hence I advise you lot larn to a greater extent than of them. They non alone assist you lot to solve mutual coding problems but too helps you lot to write amend in addition to flexible code. If you lot desire or hence assist inwards learning them, Java Design Patterns - The Complete Masterclass can hold upward used equally a companion. It's i of the amend course of educational activity on Udemy on this topic.
In this diagram, I conduct maintain explained the strategy pattern pattern using sorting algorithm like Bubble Sort, QuickSort, Insertion Sort, and Merge Sort. For our code example, you lot tin forcefulness out supersede Strategy interface alongside Match, in addition to sorting strategy to T20, OneDay in addition to Test Matches.
In Cricket, in that place are iii pop formats T20 (20 over match), One day(50 over match) in addition to Test(5 days match). If a player, plays all iii format, he needs to suit its batting strategy to hold upward effective.
For example, T20 is all nigh scoring quickly, spell One-day international games laissez passer on a chip of fourth dimension for setting in addition to and hence scoring. In contrast of shorter version, Test agree is all nigh grinding i.e. occupying the crimp in addition to making certain your opponent gets tired, before scoring briskly.
In our Strategy pattern example, nosotros conduct maintain used an Enum to define dissimilar batting strategy. We conduct maintain defined a default play() method in addition to since Enum tin forcefulness out override methods, nosotros are overriding it on every instance i.e. on T20, ONE_DAY, in addition to TEST.
Our context class, which is named equally Player too has a play() method, which delegates to play() method of configurable Strategy. Btw, if you lot are non familiar alongside Enum inwards Java in addition to it's essential features similar overriding methods, I advise you lot bring together The Complete Java Masterclass course of educational activity on Udemy. It's i of the most up-to-date course of educational activity to larn Java in addition to late updated to embrace the latest Java version equally well.
In this article, nosotros volition larn a novel way to utilization Enum, for implementing the Strategy pattern pattern. Strategy pattern is i of the famous pattern, which takes payoff of polymorphism, to withdraw switch cases in addition to strive for the open-closed pattern principle.
Formally it encapsulates related algorithm, known equally strategy in addition to brand them interchangeable. So your Client, too known equally Context, tin forcefulness out utilization a dissimilar algorithm or strategy, without whatsoever modification.
One of the substitution advantages of Strategy pattern is it's extensibility, similar introducing a novel Strategy is equally slow equally writing a novel cast in addition to implementing Strategy interface, alongside Enum, instead of creating a split class, you lot practice a split Enum instance, which agency less publish of classes in addition to sum practice goodness if Strategy pattern.
Btw, if you lot are novel into the basis of pattern pattern in addition to hence I advise you lot larn to a greater extent than of them. They non alone assist you lot to solve mutual coding problems but too helps you lot to write amend in addition to flexible code. If you lot desire or hence assist inwards learning them, Java Design Patterns - The Complete Masterclass can hold upward used equally a companion. It's i of the amend course of educational activity on Udemy on this topic.
UML diagram of the Strategy Design Pattern
Here is the UML diagram of Strategy pattern inwards Java, Why you lot should depict UML diagram? because this is the best way to think in addition to empathize how a pattern pattern works. In fact, this is the criterion ways to communicate your pattern to your immature human being developers in addition to squad members.In this diagram, I conduct maintain explained the strategy pattern pattern using sorting algorithm like Bubble Sort, QuickSort, Insertion Sort, and Merge Sort. For our code example, you lot tin forcefulness out supersede Strategy interface alongside Match, in addition to sorting strategy to T20, OneDay in addition to Test Matches.
How to implement a Strategy pattern using Enum inwards Java
In this tutorial, nosotros volition encounter an instance of implementing a Strategy pattern using Java Enum, I would utilization a sporting example. If you lot are a cricket fan, similar me, you lot are going to connect alongside this article, but if you lot are a Soccer or Tennis fan than allow me explicate a chip nigh the example.In Cricket, in that place are iii pop formats T20 (20 over match), One day(50 over match) in addition to Test(5 days match). If a player, plays all iii format, he needs to suit its batting strategy to hold upward effective.
For example, T20 is all nigh scoring quickly, spell One-day international games laissez passer on a chip of fourth dimension for setting in addition to and hence scoring. In contrast of shorter version, Test agree is all nigh grinding i.e. occupying the crimp in addition to making certain your opponent gets tired, before scoring briskly.
In our Strategy pattern example, nosotros conduct maintain used an Enum to define dissimilar batting strategy. We conduct maintain defined a default play() method in addition to since Enum tin forcefulness out override methods, nosotros are overriding it on every instance i.e. on T20, ONE_DAY, in addition to TEST.
Our context class, which is named equally Player too has a play() method, which delegates to play() method of configurable Strategy. Btw, if you lot are non familiar alongside Enum inwards Java in addition to it's essential features similar overriding methods, I advise you lot bring together The Complete Java Masterclass course of educational activity on Udemy. It's i of the most up-to-date course of educational activity to larn Java in addition to late updated to embrace the latest Java version equally well.
Strategy Pattern Example using Enum
Here is sum code instance of implementing Strategy pattern pattern using Enum inwards Java. If you lot are coding inwards Eclipse IDE in addition to hence you lot don't involve to practice much, simply select in addition to re-create this code, select the Java projection you lot are working inwards Eclipse IDE in addition to glue it.Eclipse volition conduct maintain aid of creating the correct packages in addition to source file alongside a proper advert similar the advert of a pa world class.
This is the quickest way to endeavor in addition to execute Java code snippet from the network inwards your IDE. Also remember, the Strategy pattern is a skilful instance of Open Closed Design Principle of Enum, you lot non alone reduced the publish of classes but too achieves extensibility in addition to flexibility of Strategy pattern. On the downside, Yes, you lot involve to alter your tried in addition to tested Enum every time, though past times inserting alone novel code, it nonetheless violates Open Closed regulation a bit, which advocates that novel functionality should hold upward added past times writing novel code, rather than modifying existing code.
Further Learning
courses)
P. S. - If you lot are looking for or hence costless course of educational activity to showtime learning pattern patterns, in that place is a skilful i on Udemy called Java Design Patterns in addition to Architecture past times John Purcell. It doesn't embrace all the GOF patterns but still, it's skilful to larn all the of import ones in addition to architectures similar MVC.