策略模式(封裝一系列的功能,使之可以相互替換)

策略模式和工廠方法模式很類似(感覺策略模式是工廠方法模式的一部分)

策略模式就是將一系列的功能封裝起來(用工廠方法模式封裝),使之可以相互替換(C++多態調用),從而使功能更加獨立,與客戶程序的耦合性降低(工廠模式也是這樣的)

 

舉一個賣電腦的例子,比如有一家電腦店,平時賣華碩,惠普,過了一段時間,店主打算賣蘋果電腦,就需要添加和蘋果電腦相關的程序

 

代碼

頭文件

common.h

#ifndef COMMONHEAD
#define COMMONHEAD

#include <iostream>
#include <cassert>
using namespace std;

class saleprice
{
public:
	saleprice(){cout<<__func__<<endl;}
	virtual ~saleprice(){cout<<__func__<<endl;}

	virtual void theprice()=0;
};

class computerfactory
{
public:
	computerfactory(){cout<<__func__<<endl;}
	virtual ~computerfactory(){cout<<__func__<<endl;}

	virtual saleprice *producecomputer()=0;
};

#endif

huashuo.h

#ifndef HUASHUOHEAD
#define HUASHUOHEAD

#include "common.h"

class huashuoprice:public saleprice
{
public:
	huashuoprice() {cout<<__func__<<endl;}
	~huashuoprice() {cout<<__func__<<endl;}

	virtual void theprice();	
};

class huashuofactory:public computerfactory
{
public:
	huashuofactory();
	~huashuofactory();

	virtual saleprice *producecomputer();

private:
	saleprice *instance;
};

#endif

huipu.h

#ifndef HUIPUHEAD
#define HUIPUHEAD

#include "common.h"

class huipuprice:public saleprice
{
public:
	huipuprice(){cout<<__func__<<endl;}
	~huipuprice(){cout<<__func__<<endl;}

	virtual void theprice();
};

class huipufactory:public computerfactory
{
public:
	huipufactory();
	~huipufactory();

	virtual saleprice *producecomputer();

private:
	saleprice *instance;
};

#endif

run.h

#ifndef RUNHEAD
#define RUNHEAD 
#include "common.h"
#include "huashuo.h"
#include "huipu.h"

class run
{
public:
	run(computerfactory *factory);
	~run();
	
	void startrun();

private:
	computerfactory *m_factory;
};

#endif

 

源文件

huashuo.cpp

#include "huashuo.h"

void huashuoprice::theprice()
{
	cout<<__func__<<endl;
}	

huashuofactory::huashuofactory():
instance(nullptr)
{
	cout<<__func__<<endl;
}

huashuofactory::~huashuofactory()
{
	if (instance) {
		delete instance;
		instance=nullptr;
	}
}

saleprice* huashuofactory::producecomputer()
{
	cout<<__func__<<endl;
	instance=new huashuoprice();
	return instance;
}

huipu.cpp

#include "huipu.h"

void huipuprice::theprice()
{
	cout<<__func__<<endl;
}	

huipufactory::huipufactory():
instance(nullptr)
{
	cout<<__func__<<endl;
}

huipufactory::~huipufactory()
{
	cout<<__func__<<endl;
	if (instance) {
		delete instance;
		instance=nullptr;
	}
}

saleprice* huipufactory::producecomputer()
{
	cout<<__func__<<endl;
	instance=new huipuprice();
	return instance;
}

run.cpp

#include "run.h"

run::run(computerfactory *factory):
m_factory(factory)
{
	cout<<__func__<<endl;
}

run::~run()
{
	cout<<__func__<<endl;
	if(m_factory) {
		delete m_factory;
		m_factory=nullptr;
	}
}

void run::startrun()
{
	cout<<__func__<<endl;
	saleprice *price=m_factory->producecomputer();
	price->theprice();
}

main.cpp

#include "run.h"

int main(int argc, char const *argv[])
{
	computerfactory *instance =new huipufactory();
	run r(instance);
      r.startrun();

	return 0;
}

CMakeLists

cmake_minimum_required(VERSION 2.8)  

project( strategy )  

INCLUDE_DIRECTORIES(include)

AUX_SOURCE_DIRECTORY(src DIR_SRCS)

add_executable(strategy ./src/huashuo.cpp ./src/huipu.cpp ./src/run.cpp ./src/main.cpp) 

這些代碼實質和工廠方法模式的代碼一模一樣,運行結果如下

 

現在,添加蘋果電腦的模擬程序

apple.h

#ifndef APPLEHEAD
#define APPLEHEAD

#include "common.h"

class appleprice:public saleprice
{
public:
	appleprice() {cout<<__func__<<endl;}
	~appleprice() {cout<<__func__<<endl;}

	virtual void theprice();	
};

class applefactory:public computerfactory
{
public:
	applefactory();
	~applefactory();

	virtual saleprice *producecomputer();

private:
	saleprice *instance;
};

#endif

apple.cpp

#include "apple.h"

void appleprice::theprice()
{
	cout<<__func__<<endl;
}	

applefactory::applefactory():
instance(nullptr)
{
	cout<<__func__<<endl;
}

applefactory::~applefactory()
{
	if (instance) {
		delete instance;
		instance=nullptr;
	}
}

saleprice* applefactory::producecomputer()
{
	cout<<__func__<<endl;
	instance=new appleprice();
	return instance;
}

CMakeLists

cmake_minimum_required(VERSION 2.8)  

project( strategy )  

INCLUDE_DIRECTORIES(include)

AUX_SOURCE_DIRECTORY(src DIR_SRCS)

add_executable(strategy ./src/apple.cpp ./src/huashuo.cpp ./src/huipu.cpp ./src/run.cpp ./src/main.cpp) 

重新編譯之後的結果

可見,當有新的需求需要添加時,可以直接在原有的基類上進行擴展,而不需要更改原有代碼的邏輯,從而減少了編譯時間,新的編譯結果顯示只是將新添加的功能重新編譯。而且,新增功能(蘋果電腦模擬程序)和原有功能彼此分離,彼此可以互相替換

策略模式一般包含在工廠方法模式中,用來封裝不同的功能,使功能互相獨立而彼此又可以相互替換

但是策略模式和工廠模式相對來說都有一個缺點,就是隨着軟件的擴大,類的個數不斷增減,文件數量也不斷增加

 

參考:

https://www.bilibili.com/video/BV1kW411P7KS?p=4

《設計模式,可複用面向對象軟件的基礎》

 

歡迎大家評論交流,作者水平有限,如有錯誤,歡迎指出

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