Strategy

#include <iostream>
using namespace std;
 
class SortStrategy {
public:
	virtual void Sort() = 0;
};
 
class QuickSortStrategy : public SortStrategy {
public:
	virtual void Sort() {
		cout << "Quick Sorting..." << endl;
		cout << "Finish." << endl;
	}
};
 
class BubbleSortStrategy : public SortStrategy {
public:
	virtual void Sort() {
		cout << "Bubble Sorting..." << endl;
		cout << "Finish." << endl;
	}
};
 
 
class ContextClass {
public:
	virtual void ContextInterface(int arg) {
		SortStrategy *sort_obj;
		if (arg == 0) {
			sort_obj = new QuickSortStrategy;
		} else {
			sort_obj = new BubbleSortStrategy;
		}
		sort_obj->Sort();
		delete sort_obj;
	}
};
 
 
int main(int argc, char **argv) {
	ContextClass a_class;
	if (argc == 1) {
		a_class.ContextInterface(0);
	} else {
		a_class.ContextInterface(1);
	}
	return 0;
}
 



参考サイト

デザインパターンを“喩え話”で分かり易く理解する
http://www.netlaputa.ne.jp/~hijk/study/oo/designpattern.html



デザインパターンの骸骨たち
http://www002.upp.so-net.ne.jp/ys_oota/mdp/

デザインパターンの使い方: Strategy
http://japan.internet.com/developer/20080919/26.html
最終更新:2011年11月15日 06:25