bridge模式

看了好几天,一直没想明白bridge模式到底怎么用,知道今天把四人帮的书翻出来,看到这样一句话,才恍然大悟,敲出来记录一下。

“一般来讲,Implementor接口仅提供基本操作,而Abstraction则定义了基于这些基本操作的较高层次的操作。”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 
 5 // 为实现Abstraction定义的抽象基类,定义了实现的接口函数
 6 class Implementor
 7 {
 8 public:
 9     Implementor(){}
10     virtual ~Implementor(){}
11 
12     virtual void OperationImpl() = 0;
13 };
14 
15 // 继承自Implementor,是Implementor的不同实现之一
16 class ConcreateImplementorA
17     : public Implementor
18 {
19 public:
20     ConcreateImplementorA(){}
21     virtual ~ConcreateImplementorA(){}
22 
23     virtual void OperationImpl(){cout<<"I'm in AAAAAAAAAAAAAA!!!\n";}
24 };
25 
26 // 继承自Implementor,是Implementor的不同实现之一
27 class ConcreateImplementorB
28     : public Implementor
29 {
30 public:
31     ConcreateImplementorB(){}
32     virtual ~ConcreateImplementorB(){}
33 
34     virtual void OperationImpl(){cout<<"I'm in BBBBBBBBBBBBBBB!!!\n";}
35 };
36 class Abstraction
37 {
38 public:
39     Abstraction(Implementor* pImplementor){
40         m_pImplementor=pImplementor;
41     }
42     virtual ~Abstraction(){delete m_pImplementor;};
43 
44     void Operation(){
45         m_pImplementor->OperationImpl();
46     }
47 
48 protected:
49     Implementor* m_pImplementor;
50 };
51 
52 
53 int main()
54 {
55     Abstraction* tmp=new Abstraction(new ConcreateImplementorA());
56     tmp->Operation();
57     return 0;
58 }

posted on 2012-07-19 10:34  kakamilan  阅读(151)  评论(0编辑  收藏  举报

导航