中国 - 喜马拉雅

       Let life be beautiful like summer flowers . . .

模版方法模式是“坚持相同的代码”,而被覆盖的函数是“变化的代码”。然而,这种变化在编译时通过继承被固定下来。按照“组合优于继承”的格言,可以利用组合来解决将变化的代码从“坚持相同的代码”中分开的问题,从而产生策略(Strategy)模式。这种主法有一个明显的好外:在程序运行时,可以插入变化的代码。策略模式也加入了“语境”,它可以是一个代理类,这个类控制着对特定策略对像的选择和使用——就像状态模式一样。

“策略”的意思是:可以使用多种方法来解决某个问题——即“条条大路通逻马”。现在考虑一下忘记了某个人姓名时的情境。这里的程序可以用不同的方法解决这个问题:

#include <iostream>
using namespace std;

class NameStrategy {
public:
	virtual void greet() = 0;
};

class SayHi : public NameStrategy {
public:
	void greet() {
		cout << "Hi! How's it going?" << endl;
	}
};

class Ignore : public NameStrategy {
public:
	void greet() {
		cout << "(Pretend I don't see you)" << endl;
	}
};

class Admission : public NameStrategy {
public:
	void greet() {
		cout << "I'm sorry. I forgot your name." << endl;
	}
};

// The "Context" controls the strategy:
class Context {
	NameStrategy& strategy;
public:
	Context(NameStrategy& strat) : strategy(strat) {}
	void greet() { strategy.greet(); }
};

int main() {
	SayHi sayhi;
	Ignore ignore;
	Admission admission;
	Context c1(sayhi), c2(ignore), c3(admission);
	c1.greet();
	c2.greet();
	c3.greet();
}

在main()中可以看到,可以在运行时就策略进行选择。

选自《C++编程思想》。

posted on 2012-10-01 12:16  chinaxmly  阅读(380)  评论(0编辑  收藏  举报