q****x 发帖数: 7404 | 1 /*
Is template method in parallel with strategy to abstract different algorithms? If so, how to modify the following example to template method?
*/
#include
using namespace std;
class Robot;
class Search {
public:
virtual void apply(Robot*) = 0;
};
class Linear: public Search {
public:
void apply(Robot*) { cout << "linear" << endl;}
};
class Spiral: public Search {
public:
void apply(Robot*) { cout << "spiral" << endl;}
};
class Robot {
public:
void go() { m_search->apply(this); }
void setSearch(Search* s) { m_search = s; }
private:
Search* m_search;
};
int main()
{
Robot r;
r.setSearch(new Spiral);
r.go();
} |
H***e 发帖数: 476 | 2 个人认为像stragegy这种冷门的就没必要看了
就找几个常用的弄透
algorithms? If so, how to modify the following example to template method?
【在 q****x 的大作中提到】 : /* : Is template method in parallel with strategy to abstract different algorithms? If so, how to modify the following example to template method? : */ : #include : using namespace std; : class Robot; : class Search { : public: : virtual void apply(Robot*) = 0; : };
|
q****x 发帖数: 7404 | 3 好像亚麻爱考的chess设计是strategy的经典应用。
【在 H***e 的大作中提到】 : 个人认为像stragegy这种冷门的就没必要看了 : 就找几个常用的弄透 : : algorithms? If so, how to modify the following example to template method?
|
z****u 发帖数: 104 | 4 我就用过这么一个 pattern,还是冷门...
【在 H***e 的大作中提到】 : 个人认为像stragegy这种冷门的就没必要看了 : 就找几个常用的弄透 : : algorithms? If so, how to modify the following example to template method?
|