标签:style blog http os io 2014 ar cti
桥接模式:把事物对象和其具体行为、具体特征分离开来,使它们可以各自独立的变化。事物对象仅是一个抽象的概念。如“圆形”、“三角形”归于抽象的“形状”之下,而“画圆”、“画三角”归于实现行为的“画图”类之下,然后由“形状”调用“画图”。“形状”成为一个继承体系,“画图”成为另一个继承体系,抽象和实现两者的关系为聚合关系。UML图如下:
#include <iostream>
#include <string>
using namespace std;
// 实现
class Implementor {
public:
virtual void Operation() = 0;
};
// 具体实现A
class ConcreteImplementorA : public Implementor {
public:
void Operation()
{
cout << "执行具体实现A中的方法" << endl;
}
};
// 具体实现B
class ConcreteImplementorB : public Implementor {
public:
void Operation()
{
cout << "执行具体实现B中的方法" << endl;
}
};
// 抽象
class Abstraction {
public:
void SetImplementor(Implementor *i)
{
implementor = i;
}
virtual void Operation() = 0;
~Abstraction()
{
delete implementor;
}
protected:
Implementor *implementor; // 包含一个实现
};
// 被提炼的抽象
class RefineAbstraction : public Abstraction {
public:
void Operation()
{
implementor->Operation();
}
};
int main()
{
Abstraction *ab = new RefineAbstraction();
ab->SetImplementor(new ConcreteImplementorA());
ab->Operation();
ab->SetImplementor(new ConcreteImplementorB());
ab->Operation();
// 别忘记删除指针
delete ab;
system("pause");
return 0;
}标签:style blog http os io 2014 ar cti
原文地址:http://blog.csdn.net/nestler/article/details/38433999