Design Patterns: strategy mode

Idea: the abstract algorithm, and then use the bridge mode uses algorithms abstract interface to achieve the purpose of the overall replacement algorithm

Understanding: and bridge mode the same, just different sides of the bridge separate ideas

example:

class Algrithm  //算法的抽象
{
public:
	virtual void algrithm() = 0;
};

class AlgrithmA: public Algrithm
{
public:
	void algrithm()
	{
		cout << "AlgrithmA" << endl;
	}
};

class AlgrithmB: public Algrithm
{
public:
	void algrithm()
	{
		cout << "AlgrithmB" << endl;
	}
};
class Content
{
	Algrithm* pAlgrithm;   //桥接模式
public:
	Content(Algrithm* pAlgrithm)
	{
		this->pAlgrithm = pAlgrithm;
	}
	
	void done()
	{
		this->pAlgrithm->algrithm();
	}
};
int main() 
{
	Content t1(new AlgrithmA());
	t1.done();
	
	Content t2(new AlgrithmB());
	t2.done();
	
	return 0;
}

Guess you like

Origin www.cnblogs.com/chusiyong/p/11433326.html