Java設計模式之(十四)——策略模式


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、策略模式定義

image-20210923211729495

①、Context封裝角色

它也叫做上下文角色, 起承上啟下封裝作用, 屏蔽高層模塊對策略、 算法的直接訪問,封裝可能存在的變化。

②、Strategy 抽象策略角色

策略、 算法家族的抽象, 通常為接口, 定義每個策略或算法必須具有的方法和屬性。

③、ConcreteStrategy 具體策略角色

實現抽象策略中的操作, 該類含有具體的算法。

3、策略模式通用代碼

public class Context {
    // 抽象策略
    private Strategy strategy = null;
    // 構造函數設置具體策略
    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);
        // 執行封裝后的方法
        context.doAnything();
    }
}

4、用策略模式改寫if-else

假設我們要處理一個office文件,分為三種類型 docx、xlsx、pptx,分別表示Word文件、Excel文件、PPT文件,根據文件后綴分別解析。

4.1 常規寫法

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、策略模式優點

①、算法可以自由切換

這是策略模式本身定義的, 只要實現抽象策略, 它就成為策略家族的一個成員, 通過封裝角色對其進行封裝, 保證對外提供“可自由切換”的策略。

②、避免使用多重條件判斷

簡化多重if-else,或多個switch-case分支。

③、擴展性良好

增加一個策略,只需要實現一個接口即可。

5、策略模式應用場景

①、多個類只有在算法或行為上稍有不同的場景。

②、算法需要自由切換的場景。

③、需要屏蔽算法規則的場景。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM