成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何通過策略模式簡化 if-else?

開發(fā) 前端
假設(shè)我們要處理一個(gè)office文件,分為三種類型 docx、xlsx、pptx,分別表示W(wǎng)ord文件、Excel文件、PPT文件,根據(jù)文件后綴分別解析。

哈嘍,大家好,我是指北君。

相信大家日常開發(fā)中會(huì)經(jīng)常寫各種分支判斷語句,比如 if-else ,當(dāng)分支較多時(shí),代碼看著會(huì)比較臃腫,那么如何優(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):定義一族算法類,將每個(gè)算法分別封裝起來,讓它們可以互相替換。

2.策略模式定義

圖片

①Context封裝角色

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

②Strategy 抽象策略角色

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

③ConcreteStrategy 具體策略角色

實(shí)現(xiàn)抽象策略中的操作, 該類含有具體的算法。

3.策略模式通用代碼

public class Context {
// 抽象策略
private Strategy strategy = null;
// 構(gòu)造函數(shù)設(shè)置具體策略
public Context(Strategy strategy) {
this.strategy = strategy;
}
// 封裝后的策略方法
public void doAnything(){
this.strategy.doSomething();
}
}
public interface Strategy {
// 策略模式的運(yùn)算法則
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) {
// 聲明一個(gè)具體的策略
Strategy strategy = new ConcreteStrategy1();
// 聲明上下文對象
Context context = new Context(strategy);
// 執(zhí)行封裝后的方法
context.doAnything();
}
}

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

假設(shè)我們要處理一個(gè)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){
// 解析文件名獲取文件擴(kuò)展名,比如 文檔.docx,返回 docx
String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
return fileExtension;
}
}

處理邏輯全部放在一個(gè)類中,會(huì)導(dǎo)致整個(gè)類特別龐大,假設(shè)我們要新增一種類型處理,比如對于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){
// 解析文件名獲取文件擴(kuò)展名,比如 文檔.docx,返回 docx
String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
return fileExtension;
}
}

4.策略模式優(yōu)點(diǎn)

①算法可以自由切換

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

②避免使用多重條件判斷

簡化多重if-else,或多個(gè)switch-case分支。

③擴(kuò)展性良好

增加一個(gè)策略,只需要實(shí)現(xiàn)一個(gè)接口即可。

5.策略模式應(yīng)用場景

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

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

③需要屏蔽算法規(guī)則的場景。

責(zé)任編輯:武曉燕 來源: Java技術(shù)指北
相關(guān)推薦

2023-06-02 07:30:24

If-else結(jié)構(gòu)流程控制

2025-06-26 01:10:00

服務(wù)定位解析器Spring

2020-09-27 14:24:58

if-else cod業(yè)務(wù)

2013-03-06 10:28:57

ifJava

2022-04-12 07:32:40

引擎模式Spring策略模式

2020-10-22 09:20:22

SQLNoSQL 數(shù)據(jù)庫

2021-04-13 06:39:13

代碼重構(gòu)code

2021-03-10 07:20:43

if-else靜態(tài)代碼

2020-12-15 09:31:58

CTOif-else代碼

2020-05-13 14:15:25

if-else代碼前端

2021-11-04 08:53:00

if-else代碼Java

2025-04-24 08:40:00

JavaScript代碼return語句

2020-07-17 13:01:44

If-Else代碼編程

2020-12-29 09:16:36

程序員對象開發(fā)

2021-12-07 11:31:47

Python代碼if…elif…els

2020-04-09 08:29:50

編程語言事件驅(qū)動(dòng)

2024-06-18 18:36:03

2021-01-29 07:45:27

if-else代碼數(shù)據(jù)

2020-04-05 10:27:04

設(shè)計(jì)模式結(jié)構(gòu)

2021-05-17 14:57:23

策略模式代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: h片在线看 | 中文字幕国产 | 一区在线观看视频 | 99亚洲精品 | 久久宗合色 | 免费不卡视频 | 在线日韩视频 | 插插宗合网| www.久久久久久久久久久 | 日韩美女在线看免费观看 | 日韩精品在线免费观看视频 | 亚洲精品久久久 | 午夜久久久久久久久久一区二区 | 天天插天天操 | 91免费在线 | 亚洲国产精品一区二区久久 | 欧美视频二区 | 欧美激情 一区 | 少妇午夜一级艳片欧美精品 | 欧美精品一区在线 | 日韩欧美一区二区三区四区 | 精品国产视频 | 福利网址| 国产999精品久久久久久 | 久久r免费视频 | 91在线视频在线观看 | 婷婷在线视频 | 中文字幕日韩在线观看 | 欧美啪啪 | 亚洲av毛片 | 99视频在线免费观看 | 日韩高清一区二区 | 日韩一级不卡 | 国产精品国产成人国产三级 | 免费看日韩视频 | 福利视频日韩 | 国产激情一区二区三区 | 国产在线一区二区三区 | 日韩精品一区二区三区中文在线 | 午夜小电影 | 日本不卡高清视频 |