SPRING中策略模式使用的正確姿態

2021-10-19 02:29:33 字數 1207 閱讀 1347

在上述簡介中的場景咱們任取乙個, 比如在物流專案中,整合各個電商平台然後進行訂單收件人資訊解密策略。

(1)定義策略介面

public inte***ce idecryptstrategyservice
(2)實現定義的策略的具體實現

@service

public class pdddecryptstrategyservice implements idecryptstrategyservice

}

@service

public class jddecryptstrategyservice implements idecryptstrategyservice

}

(4)實現定義的策略選擇器的實現

@service

public class decryptstrategyselector

}public class enums

public string getcode()

public void setcode(string code)

public string getstrategyname()

public void setstrategyname(string strategyname)

private static mapmap = new hashmap<>();

static

public static mapgetmap() }}

以上是在spring中如何使用策略模式

(1)以上的策略模式案例相對來說不並不複雜,主要的邏輯都是體現在關於不同平台的解密策略上。結構相對來說也⽐較簡單,在實際的開發中這樣的設計模式也是⾮常常⽤的。另外這樣的設計與命令模式、介面卡模式結構相似,但是思路是有差異的。

(2)通過策略設計模式的使⽤可以把我們⽅法中的if語句優化掉,⼤量的if語句使⽤會讓**難以擴充套件,也不好維護,同時在後期遇到各種問題也很難維護。在使⽤這樣的設計模式後可以很好的滿⾜隔離性與和擴充套件性,對於不斷新增的需求也⾮常⽅便承接。

(3)策略模式 、 介面卡模式 、 組合模式 等,在⼀些結構上是⽐較相似的,但是每⼀個模式是有⾃⼰的邏輯特點,在使⽤的過程中最佳的⽅式是經過較多的實踐來吸取經驗,為後續的研發設計提供更好的技術輸出。

策略模式及Spring整合策略模式

抽象策略類 inte ce sortservice 具體策略類 class insertionsortserviceimpl implements sortservice else arr j 1 tmp return arr 氣泡排序 class bubblesortserviceimpl imp...

Spring中使用設計模式(一 策略模式)

前幾天看到新來的同事桌上有一本設計模式的書,想起許多年前自己啃厚厚的模式時,始終昏昏欲睡,不得要領那段時光。重整思路,結合spring,寫下現在在開發中常用的設計模式使用方式。在工作中最常用的就是策略模式了,也可以說是策略模式的簡化版。客戶端可以決定用那種策略,呼叫上下文,上下文使用具體策略做具體的...

基於spring使用策略模式消除if else

當我們的 出現很多if else的時候,這時候 看起來是非常不美觀的,所以可以使用策略模式將if else消除。這次講的主要是基於spring的 public inte ce operationpublic class addoperation implements operation overri...