Implement "GOF's Bridge pattern" Using C++(Series of Gof patterns using C++ 3rd article)

Implement "GOF's Bridge pattern" Using C++(Series of Gof patterns using C++ 3rd article)
The Gof has 23 design patterns , but in most of  the design books , the E.G is written in Java , So  from now on , I will write about 23 articles to implement them using C++ , the design pattern itself is discussible , so welcome everyone to give his excelent idea to me , My QQ: 286259397 And My MSN : [email protected].
Welcome every one to express his ideas!

 

This is the  Third Arcticle:  Bridge PATTERN

T
he Bridge pattern separate the impelment part  and the abstract part , so that they can variant each other


Implement "GOF's Bridge pattern" Using C++(Series of Gof patterns using C++ 3rd article)_第1张图片
                                 3.1          Bridge -Pattern  UML Diagram
Referring the UML , The C# Codes goes Here:

 1namespace BridgeDesignPattern
 2{
 3    abstract class TSquare
 4    {
 5        public abstract void OnKey();
 6        public abstract void Conflict();
 7        public abstract void Glitter();
 8    }
;
 9
10    class TSquareHole : TSquare
11    {
12        private TSquareImp _imp;
13        public TSquareHole(TSquareImp _imp)
14        {
15            this._imp = _imp;
16        }

17        public override void OnKey()
18        {
19            _imp.OnKey();
20        }

21        public  override void  Conflict()
22        {
23            _imp.Conflict();
24        }

25        public override void Glitter()
26        {
27            _imp.Glitter();
28        }

29    }
;
30
31    abstract class TSquareImp
32    {
33        public abstract void OnKey();
34        public abstract void Conflict();
35        public abstract void Glitter();
36
37    }
;
38    class TPCSquareHoleImp : TSquareImp
39    {
40        public override void OnKey()
41        {
42            Console.WriteLine("TPCSquareHoleImp On key");
43
44        }

45        public override void Conflict()
46        {
47            Console.WriteLine("TPCSquareHoleImp Conflict");
48        }

49        public override void Glitter()
50        {
51            Console.WriteLine("TPCSquareHoleImp Glitter");
52        }

53    }
;
54    class TMobileSquareHoleImp:TSquareImp
55    {
56            public override void OnKey()
57            {
58                Console.WriteLine("TMobileSquareHoleImp On key");
59
60            }

61            public override void Conflict()
62            {
63                Console.WriteLine("TMobileSquareHoleImp Conflict");
64            }

65            public override void Glitter()
66            {
67                Console.WriteLine("TMobileSquareHoleImp Glitter");
68            }

69    }
;
            

  static   void  Main( string [] args)
           
{
                  BridgeDesignPattern.TMobileSquareHoleImp _imp 
= new BridgeDesignPattern.TMobileSquareHoleImp();
                  BridgeDesignPattern.TSquareHole _hole 
= new BridgeDesignPattern.TSquareHole(_imp);
                  _hole.OnKey();

               Console.Read();
        }



71 }


And there is the C++ Code:

 1 /**/ /********************************************************************
 2    created:    2007/04/24
 3    created:    24:4:2007   2:34
 4    filename:     C:\TESTVC6\TESTSS.CPP
 5    file path:    C:\TESTVC6
 6    file base:    TESTSS
 7    file ext:    CPP
 8    author:        常兴龙(King.C)
 9    
10    purpose:    桥模式
11*********************************************************************/

12
13 #include  < iostream >
14 #include  < vector >
15
16 using   namespace  std;
17 class  T
18 {
19public:
20    virtual void onKey()=0;
21}
;
22 class  TImp
23 {
24public:
25    virtual void OnKeyImp()=0;
26}
;
27 class  TT: public  T
28 {
29public:
30    TT(TImp *_imp)
31    {
32        this->_imp=_imp;
33    }

34    void onKey()
35    {
36        _imp->OnKeyImp();
37    }

38private:
39  TImp * _imp;
40}
;
41 class  T40Imp: public  TImp
42 {
43public:
44    void OnKeyImp()
45    {
46        cout<<"t40 imp"<<endl;
47    }

48}
;
49 class  T50Imp: public  TImp
50 {
51public:
52    void OnKeyImp()
53    {
54        cout<<"t50 imp"<<endl;
55    }

56}
;
57 void  main()
58 {
59    TImp *_imp=new T40Imp();
60
61    TT _tt(_imp);
62    TImp *_imp2=new T50Imp();
63    TT _tt2(_imp2);
64    _tt2.onKey();
65    _tt.onKey();
66    
67    return;
68}

你可能感兴趣的:(Implement "GOF's Bridge pattern" Using C++(Series of Gof patterns using C++ 3rd article))