Understand the term
The Strategy Design Pattern is a behavioral Pattern. Defined in Design Patterns as:
Define a cluster of algorithm classes and encapsulate each algorithm individually so that they can be replaced with each other. Policy patterns can make changes to algorithms independent of the clients that use them.
The word strategy can be understood here to mean specific solutions to different problems. In code, a policy is a call to a specialized handler for each input condition.
role
Definition, creation, and use of decoupling policies in code.
-
Definition: contains a policy interface and a set of policy classes that implement the interface
-
Creation: This is done by the factory class and encapsulates the details of policy creation
-
Use: The client dynamically selects which policy class to use at run time
The strategy pattern is not designed to eliminate if-else in nature. What can be used to eliminate if-else is table lookup, custom annotations.
Applicable scenario
There are a large number of if-else statements in the program, which execute different logic according to different conditions. These logic coupling in a method makes the program appear bloated, complicated logic, poor readability, and difficult to maintain the code.
Code sample
There is a requirement to implement a program that sorts all the numbers in a given file. According to the file size, we should adopt different sorting algorithms. According to the file size, the algorithm should be used in order of fast sorting, external sorting, external parallel sorting, distributed MapReduce sorting.
Definition:
// Define the policy class interface, in this case define a sorting algorithm interface
public Interface ISortAlg {
void sort(String filePath);
}
// Define the sorting algorithm implementation class
public class QuickSort implements ISortAlg {
@Override
public void sort(String filePath) {
// Fast queue implementation}}public class ExternalSort implements ISortAlg {
@Override
public void sort(String filePath) {
// External sort implementation}}public class ConcurrentExternalSort implements ISortAlg {
@Override
public void sort(String filePath) {
// External parallel sort implementation}}public class MapReduceSort implements ISortAlg {
@Override
public void sort(String filePath) {
// Distributed sorting implementation}}Copy the code
Create:
// Define policy factories to create policy objects
public class SortAlgFactory {
private static final Map<String, ISortAlg> algs = new HashMap<>();
static {
algs.put("QuickSort".new QuickSort());
algs.put("ExternalSort".new ExternalSort());
algs.put("ConcurrentExternalSort".new ConcurrentExternalSort());
algs.put("MapReduceSort".new MapReduceSort());
}
public static ISortAlg getSortAlg(String type) {
returnalgs.get(type); }}Copy the code
Use:
public class Sorter {
private static final long GB = 1000 * 1000 * 1000;
public void sortFile(String filePath) {
File file = new File(filePath);
long fileSize = file.length();
ISortAlg sortAlg;
if (fileSize < 6 * GB) {
sortAlg = SortAlgFactory.getSortAlg("QuickSort");
} else if (fileSize < 10 * GB) {
sortAlg = SortAlgFactory.getSortAlg("ExternalSort");
} else if (fileSize < 100 * GB) {
sortAlg = SortAlgFactory.getSortAlg("ConcurrentExternalSort");
} else {
sortAlg = SortAlgFactory.getSortAlg("MapReduceSort"); } sortAlg.sort(filePath); }}Copy the code
As you can see, it doesn’t eliminate if-else, but it makes the code clean, logical, and maintainable.