如何通過策略模式簡化 if-else?
哈嘍,大家好,我是指北君。
相信大家日常開發(fā)中會經(jīng)常寫各種分支判斷語句,比如 if-else ,當分支較多時,代碼看著會比較臃腫,那么如何優(yōu)化呢?
1.什么是策略模式?
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
策略模式(Strategy Pattern):定義一族算法類,將每個算法分別封裝起來,讓它們可以互相替換。
2.策略模式定義
①Context封裝角色
它也叫做上下文角色, 起承上啟下封裝作用, 屏蔽高層模塊對策略、 算法的直接訪問,封裝可能存在的變化。
②Strategy 抽象策略角色
策略、 算法家族的抽象, 通常為接口, 定義每個策略或算法必須具有的方法和屬性。
③ConcreteStrategy 具體策略角色
實現(xiàn)抽象策略中的操作, 該類含有具體的算法。
3.策略模式通用代碼
public class Context {
// 抽象策略
private Strategy strategy = null;
// 構造函數(shù)設置具體策略
public Context(Strategy strategy) {
this.strategy = strategy;
}
// 封裝后的策略方法
public void doAnything(){
this.strategy.doSomething();
}
}
public interface Strategy {
// 策略模式的運算法則
public void doSomething();
}
public class ConcreteStrategy1 implements Strategy{
@Override
public void doSomething() {
System.out.println("ConcreteStrategy1");
}
}
public class ConcreteStrategy2 implements Strategy{
@Override
public void doSomething() {
System.out.println("ConcreteStrategy2");
}
}
測試:
public class StrategyClient {
public static void main(String[] args) {
// 聲明一個具體的策略
Strategy strategy = new ConcreteStrategy1();
// 聲明上下文對象
Context context = new Context(strategy);
// 執(zhí)行封裝后的方法
context.doAnything();
}
}
4.用策略模式改寫if-else
假設我們要處理一個office文件,分為三種類型 docx、xlsx、pptx,分別表示W(wǎng)ord文件、Excel文件、PPT文件,根據(jù)文件后綴分別解析。
4.1 常規(guī)寫法
public class OfficeHandler {
public void handleFile(String filePath){
if(filePath == null){
return;
}
String fileExtension = getFileExtension(filePath);
if(("docx").equals(fileExtension)){
handlerDocx(filePath);
}else if(("xlsx").equals(fileExtension)){
handlerXlsx(filePath);
}else if(("pptx").equals(fileExtension)){
handlerPptx(filePath);
}
}
public void handlerDocx(String filePath){
System.out.println("處理docx文件");
}
public void handlerXlsx(String filePath){
System.out.println("處理xlsx文件");
}
public void handlerPptx(String filePath){
System.out.println("處理pptx文件");
}
private static String getFileExtension(String filePath){
// 解析文件名獲取文件擴展名,比如 文檔.docx,返回 docx
String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
return fileExtension;
}
}
處理邏輯全部放在一個類中,會導致整個類特別龐大,假設我們要新增一種類型處理,比如對于2007版之前的office文件,后綴分別是 doc/xls/ppt,那我們得增加 else if 邏輯,違反了開閉原則,如何解決這種問題呢,答案就是通過策略模式。
4.2 策略模式改寫
public interface OfficeHandlerStrategy {
void handlerOffice(String filePath);
}
public class OfficeHandlerDocxStrategy implements OfficeHandlerStrategy {
@Override
public void handlerOffice(String filePath) {
System.out.println("處理docx");
}
}
// 省略 OfficeHandlerXlsxStrategy/OfficeHandlerPptxStrategy 類
public class OfficeHandlerStrategyFactory {
private static final Map<String,OfficeHandlerStrategy> map = new HashMap<>();
static {
map.put("docx",new OfficeHandlerDocxStrategy());
map.put("xlsx",new OfficeHandlerXlsxStrategy());
map.put("pptx",new OfficeHandlerPptxStrategy());
}
public static OfficeHandlerStrategy getStrategy(String type){
return map.get(type);
}
}
測試:
public class OfficeHandlerStrategyClient {
public static void main(String[] args) {
String filePath = "C://file/123.xlsx";
String type = getFileExtension(filePath);
OfficeHandlerStrategy strategy = OfficeHandlerStrategyFactory.getStrategy(type);
strategy.handlerOffice(filePath);
}
private static String getFileExtension(String filePath){
// 解析文件名獲取文件擴展名,比如 文檔.docx,返回 docx
String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
return fileExtension;
}
}
4.策略模式優(yōu)點
①算法可以自由切換
這是策略模式本身定義的, 只要實現(xiàn)抽象策略, 它就成為策略家族的一個成員, 通過封裝角色對其進行封裝, 保證對外提供“可自由切換”的策略。
②避免使用多重條件判斷
簡化多重if-else,或多個switch-case分支。
③擴展性良好
增加一個策略,只需要實現(xiàn)一個接口即可。
5.策略模式應用場景
①多個類只有在算法或行為上稍有不同的場景。
②算法需要自由切換的場景。
③需要屏蔽算法規(guī)則的場景。