This is aimed at beginning, intermediate and self taught programmers who want to know something about the principles behind code they may encounter
The strategy pattern solves the problem of selecting one of a range of possible actions depending on an input datum. It can be regarded as an automaton and can be related to rule based computing frameworks such as Open Rules.
The simplest way to implement the Strategy pattern in Java is probably to use the ENUM class thus decoupling the response from strategy selection.
Rule Based computing
Rule based computing is essentially a set of conditions against which data is tested. For example email filters are human curated rules and some banks allow you to raise your overdraft on line but require you to go to a branch to reduce your overdraft. Some rules may transform the data or infer something from the data, for example in Formal logical languages you may encounter
MALE(X) or FEMALE(X) AND not (ANIMAL(X)) → HUMAN(X)
Data that matches any of rule triggers an action. In most cases it is not necessary to test a datum against all rules. If the data does not match any rules no action is taken.
From the coding perspective, data that does not match the rules must be treated carefully otherwise the program may behave unpredictably (more generally the error handling path within a programme cannot be ignored – this is why the Java SWITCH statement has a default case and data, especially data created by humans, needs continual validation).
You can learn more about rule based systems HERE and the related topic of Logic programming HERE
An Enum, short for Enumerated Type, is a list of values that function as global constants and can be considered as a list of singleton classes. Enums mayhave different names in different languages (notably Python and R) and be more powerful in some languages than others.
From here on “Enum” will refer to Java Enums.
DISCLAIMER Code here is tutorial code, deliberately incomplete and with no guarantee of correctness especially if ORACLE change their specification. It will be enough to get you started but use at your own risk. America has too many lawyers.
An enum is declared as follows
The curly braces can hold a list of values or a method, which may differ for each constant
Here is an example of including values in the braces
An Enum with Methods for each constant
This Enum delegates greetings to individual nationality classes. This ensures the Enum is minimally coupled to the classes to which it delegates.
Creating a “strategy” box in this way ensures that maintenance is needed only when adding a nationality or an action to the Enum. If you wish you can define an interface the nationality classes must implement.
The Strategy Pattern can be viewed as a “box” with data dropped in a slot. The box then decides the action performed in response to the data.
Implementing it with Enums lets the enum delegate actions to external classes thus decoupling them from the calling code and reducing the amount of maintenance needed.