解决什么问题?
利用它来避免冗长的 if-else 或者 switch 分支判断。
策略模式,全称 Strategy Design Pattern,定义一簇算法类,将每个算法分别封装起来,让它们可以互相替换。
策略的定义
包含一个策略接口和一组实现接口。因为所有的策略类都实现了相同的接口,所以,客户端代码基于接口而非实现编程,可以灵活的替换不同的策略
策略的作用
主要还是解耦策略的定义、创建和使用。控制代码的复杂度。让每个部分不至于过于复杂。
案例说明
1、原始类
package com.geek.jeep.designpattern.strategy.one;
import java.io.File;
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();
if (fileSize < 6 * GB) {
quickSort(filePath);
} else if (fileSize < 10 * GB) {
externalSort(filePath);
} else {
concurrentExternalSort(filePath);
}
}
private void quickSort(String filePath) {
//快速排序
System.out.println("filePath = " + filePath);
}
private void externalSort(String filePath) {
//外部排序
}
private void concurrentExternalSort(String filePath) {
//多线程外部排序
}
}
2、策略接口类
package com.geek.jeep.designpattern.strategy.one;
public interface ISortAlg {
void sort(String filePath);
}
3、策略实现类
package com.geek.jeep.designpattern.strategy.one;
import com.geek.jeep.annotation.SortHandlerType;
import com.geek.jeep.enums.SortType;
import org.springframework.stereotype.Service;
@Service
@SortHandlerType(SortType.QuickSort)
public class QuickSort implements ISortAlg {
public QuickSort() {
}
@Override
public void sort(String filePath) {
System.out.println("QuickSort filePath = " + filePath);
}
}
package com.geek.jeep.designpattern.strategy.one;
import com.geek.jeep.annotation.SortHandlerType;
import com.geek.jeep.enums.SortType;
import org.springframework.stereotype.Service;
@Service
@SortHandlerType(SortType.ExternalSort)
public class ExternalSort implements ISortAlg{
public ExternalSort() {
}
@Override
public void sort(String filePath) {
System.out.println("ExternalSort filePath = " + filePath);
}
}
package com.geek.jeep.designpattern.strategy.one;
import com.geek.jeep.annotation.SortHandlerType;
import com.geek.jeep.enums.SortType;
import org.springframework.stereotype.Service;
@Service
@SortHandlerType(SortType.ConcurrentExternalSort)
public class ConcurrentExternalSort implements ISortAlg{
public ConcurrentExternalSort() {
}
@Override
public void sort(String filePath) {
System.out.println("ConcurrentExternalSort filePath = " + filePath);
}
}
5、策略工厂类
package com.geek.jeep.designpattern.strategy.one;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
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());
}
public static ISortAlg getISortAlg(String type) {
if (StringUtils.isEmpty(type)) {
throw new IllegalArgumentException("type 不能为空");
}
return algs.get(type);
}
}
6、策略的使用
package com.geek.jeep.designpattern.strategy.one;
import java.io.File;
public class Sorter2 {
private static final long GB = 1000 * 1000 * 1000;
public void sortFile(String filePath) {
File file = new File(filePath);
long fileSize = file.length();
ISortAlg iSortAlg;
if (fileSize < 6 * GB) {
iSortAlg = SortAlgFactory.getISortAlg(filePath);
} else if (fileSize < 10 * GB) {
iSortAlg = SortAlgFactory.getISortAlg(filePath);
} else {
iSortAlg = SortAlgFactory.getISortAlg(filePath);
}
iSortAlg.sort(filePath);
}
}