設計模式:策略模式的一些理解

1.什麼是策略模式?

定義一系列的算法,把每一個算法封裝起來, 並且使它們可相互替換,例如:我需要推送運單軌跡給機構。如淘寶,郵管局等。

2.如何實現策略模式?

策略模式分爲3個部分分別爲:

 1)抽象策略類(StrategyImpl):策略的抽象,推送的抽象具體策略類

 2)具體的策略實現,每一種推送的具體實現。

 3)環境類(StrategyContext):用來操作策略的上下文環境。

3.策略模式的代碼實現?

第一步:定義抽象策略接口

package com.example.Strategy;

/**
 * 策略模式抽象類
 */
public interface StrategyImpl {

    //定義策略模式推送接口
    public void pushStrategyImpl();
}

第二步:具體策略類

郵管局推送實現類

package com.example.Strategy;

import org.springframework.stereotype.Component;

/**
 * 策略模式實例 例如該方法爲:推送數據到postoffice
 * @author  ss
 * 理解爲定義一系列的算法,把每一個算法封裝起來, 並且使它們可相互替換
 */
@Component("strategyPostOffice")
public class StrategyPostOffice implements StrategyImpl{

    @Override
    public void pushStrategyImpl() {
        System.out.println("這個是一個推送數據到postoffice的方法");
    }

}

淘寶推送實現類

package com.example.Strategy;

import org.springframework.stereotype.Component;

/**
 * 策略模式實例 例如該方法爲:推送數據到淘寶
 * @author  ss
 * 理解爲定義一系列的算法,把每一個算法封裝起來, 並且使它們可相互替換
 */
@Component("strategyTaoBao")
public class StrategyTaoBao implements StrategyImpl{

    @Override
    public void pushStrategyImpl() {
        System.out.println("這個是一個推送數據到淘寶的方法");
    }

}

 第三步:環境類實現

package com.example.Strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *  通過Spring將實現Strategy的實現類自動注入 根據用戶傳入strategyPostOffice/strategyTaoBao選擇對應資源池
 */
@Service
public class StrategyContext {



        @Autowired
        private final Map<String, StrategyImpl> strategyMap = new ConcurrentHashMap<>();

        public StrategyContext(Map<String, StrategyImpl> strategyMap) {
            this.strategyMap.clear();
            strategyMap.forEach((k, v)-> this.strategyMap.put(k, v));
        }

        public void getResource(String poolId){
            strategyMap.get(poolId).pushStrategyImpl();
        }
    }

第四步:測試策略模式

package com.example.Strategy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class StrategyTest {

    @Autowired
   private StrategyContext strategyContext;



    @RequestMapping("/StrategyTest")
    public void strategyTest(){
        strategyContext.getResource("strategyPostOffice");//調用推送管局的方法
        strategyContext.getResource("strategyTaoBao");//調用推送淘寶的方法
    }
}

運行springBoot 訪問地址;http://127.0.0.1:8080/StrategyTest

控制檯打印信息:

4.策略模式和工廠模式有何區別? 

一、用途不一樣
工廠是創建型模式,它的作用就是創建對象;
策略是行爲型模式,它的作用是讓一個對象在許多行爲中選擇一種行爲;
二、解決不同的問題

工廠模式是創建型的設計模式,它接受指令,創建出符合要求的實例;它主要解決的是資源的統一分發,將對象的創建完全獨立出來,讓對象的創建和具體的使用客戶無關。主要應用在多數據庫選擇,類庫文件加載等。
策略模式是爲了解決的是策略的切換與擴展,更簡潔的說是定義策略族,分別封裝起來,讓他們之間可以相互替換,策略模式讓策略的變化獨立於使用策略的客戶。

三、工廠關注結果:對象的創建,策略更關注過程:行爲的封裝

  • 工廠模式
    有一天你決定去買汽車,首先得選擇4s店,A店和B店都有汽車;
    你買了A店的汽車,付過錢後,你的汽車來了就可以直接開了。但這個汽車是怎麼來的,汽車到底怎麼拼接的,汽車在那裏生產的,你是不需要管的,你需要的是一個直接可以開的汽車。

  • 策略模式
    在4s店,你要買汽車,銷售說有標準的汽車,也可以自己去材料地生產。材料有輪胎、發動機、機架材料。工序有1、2、3工序,你自己去做吧。然後你就需要自己去做,到底那些要材料,這都你自己來決定,工序1、2、3,你是怎麼實現的,都你自己決定。最後你得到了汽車。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章