#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;
}
参考サイト
最終更新:2011年11月15日 06:25