一、策略模式介紹
1、定義與型別
定義:定義了演算法家族,分別封裝起來,讓它們之間可以互相替換,此模式讓演算法的變化不會影響到使用演算法的用戶,
可以替換掉大量的 if...else...…
型別:行為型
2、適用場景
系統有很多類,而他們的區別僅僅在于他們的行為
不同一個系統需要動態地在幾種演算法中選擇一種
3、優點
符合開閉原則
避免使用多重條件轉移陳述句
提高演算法的保密性和安全性
4、缺點
客戶端必須知道所有的策略類,并自行決定使用哪一個策略類,
產生很多策略類
5、相關設計模式
策略模式和工廠模式(工廠方法和抽象工廠)
工廠模式接受指令創建對應的物件,策略模式接受創建好的物件執行不同的行為
策略模式和狀態模式
二、代碼示例
模擬場景:店鋪針對不同的活動,呼叫不同的優惠策略
1、V1版本:
策略介面:
public interface PromotionStrategy {
void doPromotion();
}
策略實作類1(返現):
public class FanXianPromotionStrategy implements PromotionStrategy{
@Override
public void doPromotion() {
System.out.println("返現促銷,回傳的金額存放到用戶的余額中");
}
}
策略實作類2(立減):
public class LiJianPromotionStrategy implements PromotionStrategy {
@Override
public void doPromotion() {
System.out.println("立減促銷,商品的價格直接減去配置的價格");
}
}
策略實作類3(滿減):
public class ManJianPromotionStrategy implements PromotionStrategy{
@Override
public void doPromotion() {
System.out.println("滿減促銷,滿200-20元");
}
}
策略呼叫類:
public class PromotionActivity {
private PromotionStrategy promotionStrategy;
public PromotionActivity(PromotionStrategy promotionStrategy) {
this.promotionStrategy = promotionStrategy;
}
public void executePromotionStrategy(){
promotionStrategy.doPromotion();
}
}
測驗類:
public class Test {
public static void main(String[] args) {
PromotionActivity promotionActivity618 = new PromotionActivity(new LiJianPromotionStrategy());
PromotionActivity promotionActivity1111 = new PromotionActivity(new FanXianPromotionStrategy());
promotionActivity618.executePromotionStrategy();
promotionActivity1111.executePromotionStrategy();
}
}
2、V2版本(結合簡單工廠、靜態容器單例模式):
策略介面與策略實作類一樣,無需策略呼叫類,增加空策略類、策略工廠
空策略類:
public class EmptyPromotionStrategy implements PromotionStrategy{
@Override
public void doPromotion() {
System.out.println("無促銷活動");
}
}
策略工廠類:
public class PromotionStrategyFactory {
private static Map<String, PromotionStrategy> PROMOTION_STRATEGY_MAP = new HashMap<String, PromotionStrategy>();
static {
PROMOTION_STRATEGY_MAP.put(PromotionKey.LIJIAN, new LiJianPromotionStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.FANXIAN, new FanXianPromotionStrategy());
PROMOTION_STRATEGY_MAP.put(PromotionKey.MANJIAN, new ManJianPromotionStrategy());
}
private PromotionStrategyFactory(){
}
// 無促銷類
public static final EmptyPromotionStrategy NON_PROMOTION = new EmptyPromotionStrategy();
public static PromotionStrategy getPromotionStrategy(String promotionKey){
PromotionStrategy promotionStrategy = PROMOTION_STRATEGY_MAP.get(promotionKey);
return promotionStrategy == null ? NON_PROMOTION : promotionStrategy;
}
private interface PromotionKey{
String LIJIAN = "LIJIAN";
String FANXIAN = "FANXIAN";
String MANJIAN = "MANJIAN";
}
}
測驗類:
public class Test {
public static void main(String[] args) {
String promotionKey = "LIJIAN";
PromotionStrategy promotionStrategy = PromotionStrategyFactory.getPromotionStrategy(promotionKey);
promotionStrategy.doPromotion();
}
}
三、原始碼示例
1、JDK中的Comparator

在ArrayList的sort方法中,需要傳入一個Comparator,作為排序的策略

2、spring中的InstantiationStrategy

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/6014.html
標籤:設計模式
上一篇:23種設計模式之過濾模式
