Bridge 桥接模式,将抽象与其实现解耦,使它们都可以独立地变化。解耦(decouple)是指让各种事物互相独立地行事,或者至少明确地声明之间的关系。抽象(abstraction)是指不同事物之间概念上的联系方式。
Bridge 桥接模式可以避免出现过度继承的现象。
Bridge 桥接模式的通用结构图如下:
//示例代码如下,这里另外添加了两个操作
1: //Abstraction.h
2: #pragma once
3:
4: #include "AbstractionImp.h"
5:
6: // Abstraction 抽象类,维护一个 AbstractionImp 类的指针
7: class Abstraction
8: {
9: public:
10: Abstraction(AbstractionImp* pImp);
11: virtual ~Abstraction();
12:
13: virtual void Operation(int a) = 0;
14:
15: void Operation1(int a1);
16: void Operation2(int a2);
17:
18: protected:
19: AbstractionImp* m_pAbstractionImp;
20: };
21:
22: //
23: class RefinedAbstraction1 : public Abstraction
24: {
25: public:
26: RefinedAbstraction1(AbstractionImp* pImp1) : Abstraction(pImp1){ };
27: virtual ~RefinedAbstraction1(){ };
28:
29: virtual void Operation(int a);
30: void Operation1(int a1);
31: };
32: //
33: class RefinedAbstraction2 : public Abstraction
34: {
35: public:
36: RefinedAbstraction2(AbstractionImp* pImp2) : Abstraction(pImp2){ };
37: virtual ~RefinedAbstraction2(){ };
38:
39: virtual void Operation(int a);
40: void Operation2(int a2);
41: };
1: //AbstractionImp.h
2: #pragma once
3:
4: // AbstractionImp, Abstraction 的实现
5: class AbstractionImp
6: {
7: public:
8: AbstractionImp(){}
9: virtual ~AbstractionImp(){}
10:
11: virtual void OperationImpl(int x) = 0;
12: virtual void OperationImp2(int y) = 0;
13: };
14:
15: // 继承自 AbstractionImp, AbstractionImp 的不同实现之一
16: class ConcreateImplementorA : public AbstractionImp
17: {
18: public:
19: ConcreateImplementorA(){ }
20: virtual ~ConcreateImplementorA(){ }
21:
22: virtual void OperationImpl(int x);
23: virtual void OperationImp2(int y);
24: };
25:
26: // 继承自 AbstractionImp, AbstractionImp 的不同实现之一
27: class ConcreateImplementorB : public AbstractionImp
28: {
29: public:
30: ConcreateImplementorB(){ }
31: virtual ~ConcreateImplementorB(){ }
32:
33: virtual void OperationImpl(int x);
34: virtual void OperationImp2(int y);
35: };
1: //Abstraction.cpp
2: #include "Abstraction.h"
3: #include
4:
5: Abstraction::Abstraction(AbstractionImp* pImp)
6: : m_pAbstractionImp(pImp)
7: {
8: }
9:
10: Abstraction::~Abstraction()
11: {
12: if(m_pAbstractionImp)
13: delete m_pAbstractionImp;
14: m_pAbstractionImp = NULL;
15: }
16:
17: void Abstraction::Operation1(int a1)
18: {
19: m_pAbstractionImp->OperationImpl(a1);
20: }
21: void Abstraction::Operation2(int a2)
22: {
23: m_pAbstractionImp->OperationImp2(a2);
24: }
25:
26: // 对于具体的操作,Operation1, Operattion2 可以实现更加具体的操作
27: void RefinedAbstraction1::Operation(int a)
28: {
29: Operation1(a);
30: //如
31: Operation1(a+2);
32: }
33: void RefinedAbstraction1::Operation1(int a1)
34: {
35: m_pAbstractionImp->OperationImpl(a1);
36: }
37:
38:
39: void RefinedAbstraction2::Operation(int a)
40: {
41: Operation2(a);
42: }
43: void RefinedAbstraction2::Operation2(int a2)
44: {
45: m_pAbstractionImp->OperationImp2(a2);
46: }
1: //AbstractionImp.cpp
2: #include "AbstractionImp.h"
3: #include
4:
5: // ConcreateImplementorA
6: void ConcreateImplementorA::OperationImpl(int x)
7: {
8: std::cout << "OperationImpl ConcreateImplementorA."
9: << " x= " << x << " x+1 = " << x+1 << std::endl;
10: }
11:
12: void ConcreateImplementorA::OperationImp2(int y)
13: {
14: std::cout << "OperationImp1 ConcreateImplementorA."
15: << " y= " << y << " y+2 = " << y+2 << std::endl;
16: }
17:
18: // ConcreateImplementorB
19: void ConcreateImplementorB::OperationImpl(int x)
20: {
21: std::cout << "OperationImp2 ConcreateImplementorB."
22: << " x= " << x << " x*x = " << x*x << std::endl;
23: }
24:
25: void ConcreateImplementorB::OperationImp2(int y)
26: {
27: std::cout << "OperationImp2 ConcreateImplementorB."
28: << " y= " << y << " y*y = " << y*y << std::endl;
29: }
1: //test.cpp
2: #include "Abstraction.h"
3: #include "AbstractionImp.h"
4:
5: #include
6:
7: int main()
8: {
9: AbstractionImp *pImplA = new ConcreateImplementorA();
10: AbstractionImp *pImplB = new ConcreateImplementorB();
11: Abstraction *pAbstraction[2];
12: pAbstraction[0] = new RefinedAbstraction1(pImplA);
13: pAbstraction[1] = new RefinedAbstraction2(pImplB);
14:
15: for(int i = 0; i < 2; i++)
16: {
17: pAbstraction[i]->Operation(i+1);
18: }
19:
20: delete []*pAbstraction;
21:
22:
23: //应该再实现一个抽象工厂用于创建具体的对象,即 AbstractionImp,进行测试
24:
25: return EXIT_SUCCESS;
26: }
输出如下:
OperationImpl ConcreateImplementorA. x= 1 x+1 = 2
OperationImpl ConcreateImplementorA. x= 3 x+1 = 4
OperationImp2 ConcreateImplementorB. y= 2 y*y = 4