三十二、策略模式

1. 簡(jiǎn)介

策略(Strategy)模式定義了一系列的算法,并將每一個(gè)算法封裝起來(lái),而且使它們還可以相互替換。策略模式讓算法獨(dú)立于使用它的客戶(hù)而獨(dú)立變化。策略模式是對(duì)算法的包裝,是把使用算法的責(zé)任和算法本身分割開(kāi)來(lái),委派給不同
的對(duì)象管理。看到策略模式的時(shí)候有的時(shí)候跟簡(jiǎn)單工廠相比較,其實(shí)有很大的迷惑性,都是繼承多態(tài)感覺(jué)沒(méi)有太大的差異性,簡(jiǎn)單工廠模式是對(duì)對(duì)象的管理,策略模式是對(duì)行為的封裝??梢韵群?jiǎn)單的看一下結(jié)構(gòu)圖:

策略模式演示圖

之前簡(jiǎn)單工廠是通過(guò)銀行卡作為例子的簡(jiǎn)單工廠將不同的銀行卡抽象出來(lái),如果在策略模式中我們可以將每張銀行卡的購(gòu)物,吃飯,住房。。作為一個(gè)簡(jiǎn)單的消費(fèi)策略抽象出來(lái),也可以以操作系統(tǒng)類(lèi)比,Windows,OS X,Linux可以作為簡(jiǎn)單的對(duì)象抽象,系統(tǒng)中都是有默認(rèn)軟件的,我們不需要管軟件的安裝,如果沒(méi)有軟件的話(huà)我們就需要自己下載,可以將軟件的安裝作為一個(gè)策略封裝起來(lái)。

抽象策略角色: 策略類(lèi),通常由一個(gè)接口或者抽象類(lèi)實(shí)現(xiàn)。
具體策略角色:包裝了相關(guān)的算法和行為。
環(huán)境角色:持有一個(gè)策略類(lèi)的引用,最終給客戶(hù)端調(diào)用。

2. 應(yīng)用場(chǎng)景

1、 多個(gè)類(lèi)只區(qū)別在表現(xiàn)行為不同,可以使用Strategy模式,在運(yùn)行時(shí)動(dòng)態(tài)選擇具體要執(zhí)行的行為。
2、 需要在不同情況下使用不同的策略(算法),或者策略還可能在未來(lái)用其它方式來(lái)實(shí)現(xiàn)。
3、 對(duì)客戶(hù)隱藏具體策略(算法)的實(shí)現(xiàn)細(xì)節(jié),彼此完全獨(dú)立。

3. 代碼演示

3.1 C語(yǔ)言實(shí)現(xiàn)

// 統(tǒng)一的文件接口
typedef struct _MoviePlay
{
    struct _CommMoviePlay* pCommMoviePlay;

}MoviePlay;

typedef struct _CommMoviePlay
{
    HANDLE hFile;
    void (*play)(HANDLE hFile);

}CommMoviePlay;

void play_avi_file(HANDLE hFile)
{
    printf("play avi file!\n");
}

void play_rmvb_file(HANDLE hFile)
{
    printf("play rmvb file!\n");
}

void play_mpeg_file(HANDLE hFile)
{
    printf("play mpeg file!\n");
}

void play_movie_file(struct MoviePlay* pMoviePlay)
{
    CommMoviePlay* pCommMoviePlay;
    assert(NULL != pMoviePlay);

    pCommMoviePlay = pMoviePlay->pCommMoviePlay;
    pCommMoviePlay->play(pCommMoviePlay->hFile);
}   

3.2 C++語(yǔ)言實(shí)現(xiàn)

Strategy.h

#include <iostream> 
#include <string> 
#include <memory> 
using namespace std; 
  
//strategy抽象類(lèi),用作接口 
class Strategy 
{ 
public: 
  virtual string substitute(string str)=0; 
  virtual ~Strategy() 
  { 
    cout<<" in the destructor of Strategy"<<endl; 
  } 
}; 
  
class ChineseStrategy:public Strategy 
{ 
public: 
  string substitute(string str) 
  { 
    int index=str.find("520"); 
    string tempstr=str.replace(index,3,"我愛(ài)你"); 
    return tempstr; 
  } 
  ~ChineseStrategy() 
  { 
    cout<<"in the destructor of ChineseStrategy"<<endl; 
  } 
}; 
  
class EnglishStrategy:public Strategy 
{ 
public: 
  string substitute(string str) 
  { 
    int index=str.find("520"); 
    string tempstr=str.replace(index,3,"i love ou"); 
    return tempstr; 
  } 
  ~EnglishStrategy() 
  { 
    cout<<" in the destructor of ChineseStrategy"<<endl; 
  } 
}; 
  
//Context類(lèi) 
 
class Translator 
{ 
private: 
  auto_ptr<Strategy> strategy; 
 
     //在客戶(hù)代碼中加入算法(stategy)類(lèi)型的指針。 
public: 
  ~Translator() 
  { 
    cout<<" in the destructor of Translator"<<endl; 
  } 
  void set_strategy(auto_ptr<Strategy> strategy) 
  { 
    this->strategy=strategy; 
  } 
  string translate(string str) 
  { 
    if(0==strategy.get()) 
      return ""; 
    return strategy->substitute(str); 
  } 
}; 

Strategy.cpp

#include "Strategy.h" 
  
int main(int argc, char *argv) 
{ 
  string str("321520"); 
  Translator *translator=new Translator; 
  //未指定strategy的時(shí)候 
  cout<<"No Strategy"<<endl; 
  translator->translate(str); 
  cout<<"---------------"<<endl; 
    
  //翻譯成中文 
  auto_ptr<Strategy> s1(new ChineseStrategy); 
  translator->set_strategy(s1); 
  cout<<"Chinese Strategy"<<endl; 
  cout<<translator->translate(str)<<endl; 
  cout<<"---------------"<<endl; 
  
  //翻譯成英文 
  auto_ptr<Strategy> s2(new EnglishStrategy); 
  translator->set_strategy(s2); 
  cout<<"English Strategy"<<endl; 
  cout<<translator->translate(str)<<endl; 
  cout<<"----------------"<<endl; 
  
  delete translator; 
  return 0; 
  
} 

3.3 OC實(shí)現(xiàn):安裝軟件

Strategy的抽象類(lèi):

@interface SoftWareStrategy : NSObject
 
-(void)installStrategy;
 
@end

繼承Strategy的Xcode的策略類(lèi):

@implementation XcodeStrategy
 
-(void)installStrategy{
    NSLog(@"Xcode安裝成功");
}
 
@end

繼承Strategy的QQ的策略類(lèi):

@implementation QQStrategy
 
-(void)installStrategy{
    NSLog(@"QQ安裝成功");
}

@end
typedef NS_OPTIONS(NSInteger, StrategyType){
    StrategyXcode,
    strategyQQ
};
 
@interface SoftWareContext : NSObject
 
-(instancetype)initWithStrategyType:(StrategyType)strategyType;
 
-(void)installResult;
 
@end
@interface  SoftWareContext()
 
@property  (strong,nonatomic) SoftWareStrategy *strategy;
 
@end
@implementation SoftWareContext
 
-(instancetype)initWithStrategyType:(StrategyType)strategyType{
    self=[super init];
    if (self) {
        switch (strategyType) {
            case StrategyXcode:
                self.strategy=[[XcodeStrategy alloc]init];
                break;
            case strategyQQ:
                self.strategy=[[QQStrategy alloc]init];
                break;
        }
    }
    return self;
}
 
-(void)installResult{
    [self.strategy installStrategy];
}
 
@end

最終的調(diào)用:

SoftWareContext  *context=[[SoftWareContext alloc]initWithStrategyType:StrategyXcode];
[context installResult];

3.4 OC實(shí)現(xiàn):播放器

不同的第三方播放器只區(qū)別在播放、暫停、停止等一系列方法的實(shí)現(xiàn)和調(diào)用上的不同。我們的需求就是在未來(lái)可能會(huì)使用不同的播放器,而這些對(duì)客戶(hù)來(lái)說(shuō)應(yīng)該是隱藏的,不關(guān)心具體細(xì)節(jié)的,彼此完全獨(dú)立的。所以,完全可以通過(guò)策略模式來(lái)解決我們的需求。下面我們看其代碼實(shí)現(xiàn)。
(1)策略模式的核心就是對(duì)算法變化的封裝。
定義一個(gè)通用算法協(xié)議,讓每個(gè)算法遵守其規(guī)則。

@protocol LHPlayerProtocol <NSObject>

/**
 *  Player開(kāi)啟視頻
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)lh_play;

/**
 *  Player暫停視頻
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)lh_pause; 

/**
 *  Player停止播放
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)lh_stop;

AVPlayer的算法封裝

#import <Foundation/Foundation.h>
#import "LHPlayerProtocol.h"

@interface LHAVPlayer : NSObject<LHPlayerProtocol>

@end

#import "LHAVPlayer.h"
#import "AVPlayer.h"

@interface LHAVPlayer ()
{
    id<AVPlayerProtocol> player;// AVPlayer播放器自身的協(xié)議
}
@end

@implementation LHAVPlayer

- (instancetype)init
{
    self = [super init];
    if (self) {
        player = [[AVPlayer alloc] init];// 初始化AVPlayer播放器對(duì)象
    }
    return self;
}

// 播放
- (NSString *)lh_play{
    return [player a_play];
}

// 暫停
- (NSString *)lh_pause{
    return [player a_pause];
}

// 停止
- (NSString *)lh_stop{
    return [player a_stop];
}

- (void)dealloc
{
    player = nil;
}

@end

IJKPlayer的算法封裝

#import <Foundation/Foundation.h>
#import "LHPlayerProtocol.h"

@interface LHIJKPlayer : NSObject<LHPlayerProtocol>

@end

#import "LHIJKPlayer.h"
#import "Ijkplayer.h"

@interface LHIJKPlayer ()
{
    id<IjkplayerProtocol> player;// IJKPlayer播放器自身的協(xié)議
}
@end

@implementation LHIJKPlayer

- (instancetype)init
{
    self = [super init];
    if (self) {
        player = [[Ijkplayer alloc] init];// 初始化IJKPlayer播放器對(duì)象
    }
    return self;
}

// 播放
- (NSString *)lh_play{
    return [player i_play];
}

// 暫停
- (NSString *)lh_pause{
    return [player i_pause];
}

// 停止
- (NSString *)lh_stop{
    return [player i_stop];
}

- (void)dealloc
{
    player = nil;
}

@end

(2)策略模式中另一個(gè)核心類(lèi)Context的定義
通用播放器類(lèi)LHPlayer的定義。根據(jù)不同的策略選擇不同的算法。

#import <Foundation/Foundation.h>
#import "LHPlayerProtocol.h"

// 播放器的類(lèi)型
typedef enum : NSUInteger {
    EPlayerType_AVPlayer,
    EPlayerType_IJKPlayer
} EPlayerType;

@interface LHPlayer : NSObject

- (instancetype)initWithType:(EPlayerType)type;

/**
 *  開(kāi)啟視頻
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)play;

/**
 *  暫停視頻
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)pause; 

/**
 *  停止播放
 *
 *  @return 描述(只為舉例需要)
 */
- (NSString *)stop; 

@end

#import "LHPlayer.h"
#import "LHPlayerProtocol.h"
#import "LHAVPlayer.h"
#import "LHIJKPlayer.h"

@interface LHPlayer ()
{
    id<LHPlayerProtocol>  player;
}
@end

@implementation LHPlayer

- (instancetype)initWithType:(EPlayerType)type
{
    self = [super init];
    if (self) {
        [self initPlayerWithType:type];
    }
    return self;
}

// 初始化播放器
- (void)initPlayerWithType:(EPlayerType)type{
    switch (type) {
        case EPlayerType_AVPlayer:
        {
            player = [[LHAVPlayer alloc] init];
            break;
        }
        case EPlayerType_IJKPlayer:
        {
            player = [[LHIJKPlayer alloc] init];
            break;
        }
    }
}

//開(kāi)啟視頻
- (NSString *)play{
    return [player lh_play];
}

//暫停視頻
- (NSString *)pause{
    return [player lh_pause];
}

//停止播放
- (NSString *)stop{
    return [player lh_stop];
}

@end

下面看客戶(hù)端的調(diào)用

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

#import "ViewController.h"
#import "LHPlayer.h"

@interface ViewController ()
{
    LHPlayer *player;
}

@property (weak, nonatomic) IBOutlet UIButton *btnAVPlayer;
@property (weak, nonatomic) IBOutlet UIButton *btnIjkplayer;
@property (weak, nonatomic) IBOutlet UILabel *lbState;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initPlayerWithType:EPlayerType_IJKPlayer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 初始化播放器
- (void)initPlayerWithType:(EPlayerType)type{
    if (player) {
        player = nil;
    }
    player = [[LHPlayer alloc] initWithType:type];
}

#pragma mark -
#pragma makr Button Event

// 選擇AVPlayer
- (IBAction)btnAVPlayerEvent:(UIButton *)sender {
    sender.selected = YES;
    _btnIjkplayer.selected = NO;

    [self initPlayerWithType:EPlayerType_AVPlayer];
}

// 選擇Ijkplayer
- (IBAction)btnIjkplayerEvent:(UIButton *)sender {
    sender.selected = YES;
    _btnAVPlayer.selected = NO;

    [self initPlayerWithType:EPlayerType_IJKPlayer];
}

// 播放器播放視頻
- (IBAction)btnPlayerEvent:(UIButton *)sender {
    _lbState.text = player ? [player play] : @"播放器為空";
}

// 播放器暫停視頻
- (IBAction)btnPauseEvent:(UIButton *)sender {
    _lbState.text = player ? [player pause] : @"播放器為空";
}

// 播放器停止視頻
- (IBAction)btnStopEvent:(UIButton *)sender {
    _lbState.text = player ? [player stop] : @"播放器為空";
}

@end

大家可以看到,客戶(hù)端切換播放器只要替換一下枚舉值就可以了輕松切換了,而且哪個(gè)播放器火了,擴(kuò)展新的播放器也是輕而易舉,不對(duì)客戶(hù)端造成任何影響。這就是策略模式的好處所在。

3.5 php中的應(yīng)用

<?php
 
/**
*策略模式
*定義一系列的算法,把每一個(gè)算法封裝起來(lái),并且使它們可相互替換。
*本模式使得算法可獨(dú)立于使用它的客戶(hù)變化
*/
 
/**
*出行旅游
*/
interface TravelStrategy{
    public function travelAlgorithm();
}
 
/**
*具體策略類(lèi)(ConcreteStrategy)
*1:乘坐飛機(jī)
*/
class AirPlanelStrategy implements TravelStrategy{
    public function travelAlgorithm(){
        echo"travelbyAirPlain","<BR>\r\n";
    }
}
 
/**
*具體策略類(lèi)(ConcreteStrategy)
*2:乘坐火車(chē)
*/
class TrainStrategy implements TravelStrategy{
    public function travelAlgorithm(){
        echo"travelbyTrain","<BR>\r\n";
    }
}
/**
*具體策略類(lèi)(ConcreteStrategy)
*3:騎自行車(chē)
*/
class BicycleStrategy implements TravelStrategy{
    public function travelAlgorithm(){
        echo"travelbyBicycle","<BR>\r\n";
    }
}
 
/**
*
*環(huán)境類(lèi)(Context):
*用一個(gè)ConcreteStrategy對(duì)象來(lái)配置。
*維護(hù)一個(gè)對(duì)Strategy對(duì)象的引用??啥x一個(gè)接口來(lái)讓Strategy訪(fǎng)問(wèn)它的數(shù)據(jù)。
*算法解決類(lèi),以提供客戶(hù)選擇使用何種解決方案:
*/
class PersonContext{
    private $_strategy = null;
    public function __construct(TravelStrategy $travel){
        $this->_strategy=$travel;
    }
    /**
    *旅行
    */
    public function setTravelStrategy(TravelStrategy $travel){
        $this->_strategy=$travel;
    }
    /**
    *旅行
    */
    public function travel(){
        return $this->_strategy->travelAlgorithm();
    }
 
}
//乘坐火車(chē)旅行
$person=new PersonContext(new TrainStrategy());
$person->travel();
 
//改騎自行車(chē)
$person->setTravelStrategy(new BicycleStrategy());
$person->travel();
?>

3.6 java中的應(yīng)用

假設(shè)現(xiàn)在要設(shè)計(jì)一個(gè)販賣(mài)各類(lèi)書(shū)籍的電子商務(wù)網(wǎng)站的購(gòu)物車(chē)系統(tǒng)。一個(gè)最簡(jiǎn)單的情況就是把所有貨品的單價(jià)乘上數(shù)量,但是實(shí)際情況肯定比這要復(fù)雜。比如,本網(wǎng)站可能對(duì)所有的高級(jí)會(huì)員提供每本20%的促銷(xiāo)折扣;對(duì)中級(jí)會(huì)員提供每本10%的促銷(xiāo)折扣;對(duì)初級(jí)會(huì)員沒(méi)有折扣。

根據(jù)描述,折扣是根據(jù)以下的幾個(gè)算法中的一個(gè)進(jìn)行的:

算法一:對(duì)初級(jí)會(huì)員沒(méi)有折扣。

算法二:對(duì)中級(jí)會(huì)員提供10%的促銷(xiāo)折扣。

算法三:對(duì)高級(jí)會(huì)員提供20%的促銷(xiāo)折扣。

使用策略模式來(lái)實(shí)現(xiàn)的結(jié)構(gòu)圖如下:

計(jì)算折扣邏輯圖

抽象折扣類(lèi)

public interface MemberStrategy {
    /**
     * 計(jì)算圖書(shū)的價(jià)格
     * @param booksPrice    圖書(shū)的原價(jià)
     * @return    計(jì)算出打折后的價(jià)格
     */
    public double calcPrice(double booksPrice);
}

初級(jí)會(huì)員折扣類(lèi)

public class PrimaryMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {
        
        System.out.println("對(duì)于初級(jí)會(huì)員的沒(méi)有折扣");
        return booksPrice;
    }

}

中級(jí)會(huì)員折扣類(lèi)

public class IntermediateMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {

        System.out.println("對(duì)于中級(jí)會(huì)員的折扣為10%");
        return booksPrice * 0.9;
    }

}

高級(jí)會(huì)員折扣類(lèi)

public class AdvancedMemberStrategy implements MemberStrategy {

    @Override
    public double calcPrice(double booksPrice) {
        
        System.out.println("對(duì)于高級(jí)會(huì)員的折扣為20%");
        return booksPrice * 0.8;
    }
}

價(jià)格類(lèi)

public class Price {
    //持有一個(gè)具體的策略對(duì)象
    private MemberStrategy strategy;
    /**
     * 構(gòu)造函數(shù),傳入一個(gè)具體的策略對(duì)象
     * @param strategy    具體的策略對(duì)象
     */
    public Price(MemberStrategy strategy){
        this.strategy = strategy;
    }
    
    /**
     * 計(jì)算圖書(shū)的價(jià)格
     * @param booksPrice    圖書(shū)的原價(jià)
     * @return    計(jì)算出打折后的價(jià)格
     */
    public double quote(double booksPrice){
        return this.strategy.calcPrice(booksPrice);
    }
}

客戶(hù)端

public class Client {

    public static void main(String[] args) {
        //選擇并創(chuàng)建需要使用的策略對(duì)象
        MemberStrategy strategy = new AdvancedMemberStrategy();
        //創(chuàng)建環(huán)境
        Price price = new Price(strategy);
        //計(jì)算價(jià)格
        double quote = price.quote(300);
        System.out.println("圖書(shū)的最終價(jià)格為:" + quote);
    }

}

3.7 C#實(shí)現(xiàn)

namespace StrategyPattern
{
    // 所得稅計(jì)算策略
    public interface ITaxStragety
    {
        double CalculateTax(double income);
    }
 
    // 個(gè)人所得稅
    public class PersonalTaxStrategy : ITaxStragety
    {
        public double CalculateTax(double income)
        {
            return income * 0.12;
        }
    }
 
    // 企業(yè)所得稅
    public class EnterpriseTaxStrategy : ITaxStragety
    {
        public double CalculateTax(double income)
        {
            return (income - 3500) > 0 ? (income - 3500) * 0.045 : 0.0;
        }
    }
 
    public class InterestOperation
    {
        private ITaxStragety m_strategy;
        public InterestOperation(ITaxStragety strategy)
        {
            this.m_strategy = strategy;
        }
 
        public double GetTax(double income)
        {
            return m_strategy.CalculateTax(income);
        }
    }
 
    class App
    {
        static void Main(string[] args)
        {
            // 個(gè)人所得稅方式
            InterestOperation operation = new InterestOperation(new PersonalTaxStrategy());
            Console.WriteLine("個(gè)人支付的稅為:{0}", operation.GetTax(5000.00));
 
            // 企業(yè)所得稅
            operation = new InterestOperation(new EnterpriseTaxStrategy());
            Console.WriteLine("企業(yè)支付的稅為:{0}", operation.GetTax(50000.00));
 
            Console.Read();
        }
    }
}

4. 特性總結(jié)

*策略模式的重心

策略模式的重心不是如何實(shí)現(xiàn)算法,而是如何組織、調(diào)用這些算法,從而讓程序結(jié)構(gòu)更靈活,具有更好的維護(hù)性和擴(kuò)展性。

  • 算法的平等性

策略模式一個(gè)很大的特點(diǎn)就是各個(gè)策略算法的平等性。對(duì)于一系列具體的策略算法,大家的地位是完全一樣的,正因?yàn)檫@個(gè)平等性,才能實(shí)現(xiàn)算法之間可以相互替換。所有的策略算法在實(shí)現(xiàn)上也是相互獨(dú)立的,相互之間是沒(méi)有依賴(lài)的。

所以可以這樣描述這一系列策略算法:策略算法是相同行為的不同實(shí)現(xiàn)。

  • 運(yùn)行時(shí)策略的唯一性

運(yùn)行期間,策略模式在每一個(gè)時(shí)刻只能使用一個(gè)具體的策略實(shí)現(xiàn)對(duì)象,雖然可以動(dòng)態(tài)地在不同的策略實(shí)現(xiàn)中切換,但是同時(shí)只能使用一個(gè)。

  • 公有的行為

經(jīng)常見(jiàn)到的是,所有的具體策略類(lèi)都有一些公有的行為。這時(shí)候,就應(yīng)當(dāng)把這些公有的行為放到共同的抽象策略角色Strategy類(lèi)里面。當(dāng)然這時(shí)候抽象策略角色必須要用Java抽象類(lèi)實(shí)現(xiàn),而不能使用接口。

這其實(shí)也是典型的將代碼向繼承等級(jí)結(jié)構(gòu)的上方集中的標(biāo)準(zhǔn)做法。

代碼集中示意圖

5. 優(yōu)點(diǎn)和缺點(diǎn)

優(yōu)點(diǎn):
(1)策略模式提供了管理相關(guān)的算法族的辦法。策略類(lèi)的等級(jí)結(jié)構(gòu)定義了一個(gè)算法或行為族。恰當(dāng)使用繼承可以把公共的代碼轉(zhuǎn)移到父類(lèi)里面,從而避免重復(fù)的代碼。
(2)策略模式提供了可以替換繼承關(guān)系的辦法。繼承可以處理多種算法或行為。如果不是用策略模式,那么使用算法或行為的環(huán)境類(lèi)就可能會(huì)有一些子類(lèi),每一個(gè)子類(lèi)提供一個(gè)不同的算法或行為。但是,這樣一來(lái)算法或行為的使用者就和算法或行為本身混在一起。決定使用哪一種算法或采取哪一種行為的邏輯就和算法或行為的邏輯混合在一起,從而不可能再獨(dú)立演化。繼承使得動(dòng)態(tài)改變算法或行為變得不可能。
(3)使用策略模式可以避免使用多重條件轉(zhuǎn)移語(yǔ)句。多重轉(zhuǎn)移語(yǔ)句不易維護(hù),它把采取哪一種算法或采取哪一種行為的邏輯與算法或行為的邏輯混合在一起,統(tǒng)統(tǒng)列在一個(gè)多重轉(zhuǎn)移語(yǔ)句里面,比使用繼承的辦法還要原始和落后。
缺點(diǎn):
(1)客戶(hù)端必須知道所有的策略類(lèi),并自行決定使用哪一個(gè)策略類(lèi)。這就意味著客戶(hù)端必須理解這些算法的區(qū)別,以便適時(shí)選擇恰當(dāng)?shù)乃惴?lèi)。換言之,策略模式只適用于客戶(hù)端知道所有的算法或行為的情況。
(2)策略模式造成很多的策略類(lèi),每個(gè)具體策略類(lèi)都會(huì)產(chǎn)生一個(gè)新類(lèi)。有時(shí)候可以通過(guò)把依賴(lài)于環(huán)境的狀態(tài)保存到客戶(hù)端里面,而將策略類(lèi)設(shè)計(jì)成可共享的,這樣策略類(lèi)實(shí)例可以被不同客戶(hù)端使用。換言之,可以使用享元模式來(lái)減少對(duì)象的數(shù)量。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1 場(chǎng)景問(wèn)題# 1.1 報(bào)價(jià)管理## 向客戶(hù)報(bào)價(jià),對(duì)于銷(xiāo)售部門(mén)的人來(lái)講,這是一個(gè)非常重大、非常復(fù)雜的問(wèn)題,對(duì)不同的...
    七寸知架構(gòu)閱讀 5,243評(píng)論 9 62
  • 1 場(chǎng)景問(wèn)題 1.1 報(bào)價(jià)管理 向客戶(hù)報(bào)價(jià),對(duì)于銷(xiāo)售部門(mén)的人來(lái)講,這是一個(gè)非常重大、非常復(fù)雜的問(wèn)題,對(duì)不同的客戶(hù)要...
    4e70992f13e7閱讀 3,207評(píng)論 2 16
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 設(shè)計(jì)模式匯總 一、基礎(chǔ)知識(shí) 1. 設(shè)計(jì)模式概述 定義:設(shè)計(jì)模式(Design Pattern)是一套被反復(fù)使用、多...
    MinoyJet閱讀 4,075評(píng)論 1 15
  • 手機(jī)鈴聲響起的時(shí)候,我剛洗完澡。 沒(méi)把頭發(fā)擦干就隨便套了件睡衣,水珠順著發(fā)梢滴到我的脖子上,冰涼冰涼的。 萬(wàn)分嫌棄...
    貝兒呀貝兒閱讀 1,399評(píng)論 16 18

友情鏈接更多精彩內(nèi)容