先给出一个类的设计图

#include
using namespace std;
class Library
{
public:
void step1()
{
cout << "step1()" << endl;
}
void step3()
{
cout << "step3()" << endl;
}
void step5()
{
cout << "step5()" << endl;
}
};
class Application
{
public:
bool step2()
{
cout << "step2()" << endl;
return true;
}
bool step4()
{
cout << "step4()" << endl;
return true;
}
};
int main()
{
Library lib;
Application app;
lib.step1();
if (app.step2())
{
lib.step3();
}
if (app.step4())
{
lib.step5();
}
return 0;
}
Libray类设计#include
using namespace std;
class Library
{
public:
virtual ~Library() {};
public:
// 主流程
void run()
{
step1();
if (step2())
{
step1();
}
if (step4())
{
step5();
}
}
void step1()
{
cout << "step1()" << endl;
}
void step3()
{
cout << "step3()" << endl;
}
void step5()
{
cout << "step5()" << endl;
}
protected:
virtual bool step2() = 0;
virtual bool step4() = 0;
};
class Application : public Library
{
public:
bool step2()
{
cout << "step2()" << endl;
return true;
}
bool step4()
{
cout << "step4()" << endl;
return true;
}
};
int main()
{
Library* lib = new Application;
lib->run();
return 0;
}
Template Method 模式是一种非常基础性的设计模式,在面向对象系统中有着大量的应用。它用最简洁的机制(虚函数的多态性)为很多应用程序框架提供了灵活的扩展点,是代码复用方面的基本实现结构。Template Method的典型应用。Template Method调用的虚方法可以具有实现,也可以没有任何实现(抽象方法、纯虚方法),但一般推荐将它们设置为protected方法。enum TaxBase
{
CN_Tax,
US_Tax,
DE_Tax
// FD_Tax 增加新功能时
};
class SalesOrder
{
TaxBase tax;
public:
double Calculate()
{
if (tax == CN_Tax) {
}
else if (tax == US_Tax) {
}
else if (tax == DE_Tax) {
}
//else if (tax == FD_Tax) {} 增加新功能时
}
};
class TaxStrategy
{
public:
virtual ~TaxStrategy() {}
protected:
virtual double Calculate() = 0;
};
class CNTax : public TaxStrategy
{
public:
double Calculate() {}
};
class USTax : public TaxStrategy
{
public:
double Calculate() {}
};
// 增加功能时
// class FDTax : public TaxStrategy
// {
// public:
// double Caculate() {}
// };
Strategy及其子类为组件提供了一系列可重用的算法,从而可以使得类型在运行时方便地根据需要在各个算法之间进行切换。Strategy模式提供了用条件判断语句以外的另一种选择,消除条件判断语苟,就是在解耦合。含有许多条件判断语句的代码涌堂都需要Strategy模式。Strategy对象没有实例变量,那么各个上下文可以共享同一个Strategy对象,从而节省对象开销。#include
#include
using namespace std;
// 先声明观察者,observer是观察者
class Observer
{
public:
virtual void doObserver() = 0;
virtual ~Observer() {}
};
// 通知者,这个类可以一直不动,无论有多少个新的消息
class Observerable
{
typedef list<Observer*>::iterator listIterator;
list<Observer*> observerList;
public:
// 有消息通知过来了
void addObserver(Observer* observer) {
observerList.push_back(observer);
}
// 删除消息的通知
void removeObserver(Observer* observer) {
observerList.remove(observer);
}
void notify() {
// 对所有的消息进行处理
listIterator itor = observerList.begin();
while (itor != observerList.end()) {
(*itor)->doObserver();
itor = observerList.erase(itor);
}
}
};
class Observer_one : public Observer
{
public:
virtual void doObserver() {
cout << "." << endl;
}
};
class Observer_two : public Observer
{
public:
virtual void doObserver() {
cout << "-" << endl;
}
};
int main()
{
Observerable ob;
// 比如我现在改变了某一个值,通知几个观察者
ob.addObserver(new Observer_one);
ob.addObserver(new Observer_two);
ob.notify();
return 0;
}