设计模式:模板方法
#include <iostream>
class Abstract_Sport {
public:
void template_method() {
start();
end();
start();
end();
}
virtual void start() = 0;
virtual void end() = 0;
};
class Concrete_BasketBall : public Abstract_Sport {
void start() {
std::cout << "BasketBall start" << std::endl;
}
void end() {
std::cout << "BasketBall end" << std::endl;
}
};
class Concrete_FootBall : public Abstract_Sport {
void start() {
std::cout << "FootBall start" << std::endl;
}
void end() {
std::cout << "FootBall end" << std::endl;
}
};
int main(int argc, char* argv[]) {
std::cout << "---------" << std::endl;
Abstract_Sport* p = new Concrete_BasketBall();
p->template_method();
p->start();
p->end();
delete p;
p = NULL;
std::cout << "---------" << std::endl;
p = new Concrete_FootBall();
p->template_method();
p->start();
p->end();
delete p;
p = NULL;
return 0;
}
输出:
BasketBall start
BasketBall end
BasketBall start
BasketBall end
BasketBall start
BasketBall end
FootBall start
FootBall end
FootBall start
FootBall end
FootBall start
FootBall end