C++研究

C++细节深度探索及软件工程

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  37 随笔 :: 0 文章 :: 74 评论 :: 0 Trackbacks
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 : cxl82116@msn.com.
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



                                 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
16using namespace std;
17class T
18{
19public:
20    virtual void onKey()=0;
21}
;
22class TImp
23{
24public:
25    virtual void OnKeyImp()=0;
26}
;
27class 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}
;
41class T40Imp:public TImp
42{
43public:
44    void OnKeyImp()
45    {
46        cout<<"t40 imp"<<endl;
47    }

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

56}
;
57void 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}
posted on 2007-04-24 02:38 常兴龙 阅读(960) 评论(0)  编辑 收藏 引用 所属分类: Design Patterns & Engeering

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


> hi的博客