• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 Second Arcticle: ADAPTER PATTERN

            While
            referring to Adapter pattern , there must be a class that play the role of a adapter which can make two or more umcompatible class compatible . To explain this world , the UML diagram can make it clear easily :


                    diagram 2-1 Class Adapter UML (referred from http://terrylee.cnblogs.com)


                    diagram 2-2  Object Adapter UML (referred from http://terrylee.cnblogs.com)


            E.G Explain them sparetely:
            if a Customer request a  CUP WITH A CAP , but the orgin system can only supply a CUP , So you it's not compatible between the Request and the Orgin System , so you must find anthor factory who supply the cap ,and make it together to statisfy the customer ,  implement it in Class Adapter Pattern , Codes goes below.
            Class Adapter Pattern:

             1/********************************************************************
             2    created:    2007/04/21
             3    created:    21:4:2007   18:47
             4    filename:     c:\Visual Studio 2005\Projects\Frist2005\Frist2005\Frist2005.cpp
             5    file path:    c:\Visual Studio 2005\Projects\Frist2005\Frist2005
             6    file base:    Frist2005
             7    file ext:    cpp
             8    author:        Chang Xinglong(King.C)
             9    
            10    purpose:    Adapter Pattern -Class
            11*********************************************************************/

            12
            13#include "stdafx.h"
            14#include <map>
            15#include <iostream>
            16
            17using namespace std;
            18
            19class OrginSystem
            20{
            21
            22public:
            23    void Supply()
            24    {
            25        cout<<"Orgin:SupplyACup"<<endl;
            26    }

            27}
            ;
            28class CapSystem
            29{
            30public:
            31    void Supply()
            32    {
            33        cout<<"CapSystem:SupplyACap"<<endl;
            34    }

            35}
            ;
            36class Adapter: public OrginSystem , CapSystem
            37{
            38public:
            39    void Supply()
            40    {
            41        OrginSystem::Supply();
            42        CapSystem::Supply();
            43        cout<<"So the request has been satisfied!"<<endl;
            44    }

            45}
            ;
            46int _tmain(int argc, _TCHAR* argv[])
            47{
            48    Adapter _adpter;
            49    _adpter.Supply();
            50    system("pause");
            51    return 0;
            52}

            The Same question , implement it using Object Adapter Pattern:
             1/********************************************************************
             2    created:    2007/04/21
             3    created:    21:4:2007   18:47
             4    filename:     c:\Visual Studio 2005\Projects\Frist2005\Frist2005\Frist2005.cpp
             5    file path:    c:\Visual Studio 2005\Projects\Frist2005\Frist2005
             6    file base:    Frist2005
             7    file ext:    cpp
             8    author:        Chang Xinglong(King.C)
             9    
            10    purpose:    Adapter Pattern-Object 
            11*********************************************************************/

            12
            13#include "stdafx.h"
            14#include <map>
            15#include <iostream>
            16
            17using namespace std;
            18
            19class OrginSystem
            20{
            21
            22public:
            23    void Supply()
            24    {
            25        cout<<"Orgin:SupplyACup"<<endl;
            26    }

            27}
            ;
            28class CapSystem
            29{
            30public:
            31    void Supply()
            32    {
            33        cout<<"CapSystem:SupplyACap"<<endl;
            34    }

            35}
            ;
            36class Adapter: public OrginSystem , CapSystem
            37{
            38public:
            39    Adapter():_os(new OrginSystem),_cs(new CapSystem)
            40    {
            41
            42    }

            43    void Supply()
            44    {
            45        _os->Supply();
            46        _cs->Supply();
            47        cout<<"So the request has been satisfied!"<<endl;
            48    }

            49private:
            50    OrginSystem *_os;
            51    CapSystem * _cs;
            52}
            ;
            53int _tmain(int argc, _TCHAR* argv[])
            54{
            55    OrginSystem _orign=new _adpter();
            56    _orign.Supply();
            57    system("pause");
            58    return 0;
            59}

            60
            61

            In C++ , Muiltple-Derived pattern is not a recommend pattern, So if possible , use the Obeject -Adpter Pattern.

            That's all ,Thanks! if any different idea , send me E-mail.
            posted on 2007-04-21 19:13 常興龍 閱讀(1445) 評論(4)  編輯 收藏 引用 所屬分類: Design Patterns & Engeering

            評論

            # re: Implement "GOF's Adapter pattern" Using C++(Series of Gof patterns using C++ 2nd article) 2007-04-22 13:23 kurt
            I think the UML is enough for us to understand it  回復  更多評論
              

            # re: Implement "GOF's Adapter pattern" Using C++(Series of Gof patterns using C++ 2nd article) 2007-04-22 14:47 天津大學計算機學院 常興龍
            hehe ,thanks for your comment , it's just for the beginner study c++ systemly and this article can demostrate that how to implement interface( in Java , there is the Keyword interface) using virtual function in C++ , the uml itself didn't express this. Thanks again!  回復  更多評論
              

            # re: Implement "GOF's Adapter pattern" Using C++(Series of Gof patterns using C++ 2nd article) 2007-04-22 21:27 ★田德健★
            all are english....  回復  更多評論
              

            # re: Implement "GOF's Adapter pattern" Using C++(Series of Gof patterns using C++ 2nd article) 2007-09-12 15:47 Andrew Selivanov
            I suppose this would be more compatible version (see below)
            ----------------------------------------------------------
            #include <iostream>

            #define USE_ADAPTER
            #ifndef USE_ADAPTER

            class OrginSystem
            {

            public:
            void Supply()
            {
            std::cout<<"Orgin:SupplyACup"<<std::endl;
            }
            };
            class CapSystem
            {
            public:
            void Supply()
            {
            std::cout<<"CapSystem:SupplyACap"<<std::endl;
            }
            };
            class Adapter: public OrginSystem , CapSystem
            {
            public:
            void Supply()
            {
            OrginSystem::Supply();
            CapSystem::Supply();
            std::cout<<"So the request has been satisfied!"<<std::endl;
            }
            };

            int main()
            {
            Adapter adapter;
            adapter.Supply();
            }
            #else

            class OrginalSystem
            {

            public:

            virtual void Supply()
            {
            std::cout<<"Orgin:SupplyACup"<<std::endl;
            }
            };
            class CapSystem
            {
            public:
            virtual void Supply()
            {
            std::cout<<"CapSystem:SupplyACap"<<std::endl;
            }
            };
            class Adapter: public OrginalSystem , CapSystem
            {
            OrginalSystem* _os;
            CapSystem* _cs;

            public:
            Adapter():_os(new OrginalSystem),_cs(new CapSystem)
            {

            }
            void Supply()
            {
            _os->Supply();
            _cs->Supply();
            std::cout<<"So the request has been satisfied!"<<std::endl;
            }
            };

            int main()
            {
            OrginalSystem* original = new Adapter();
            original->Supply();
            delete original;
            }

            #endif
            ----------------------------------------------------------
            Andrew  回復  更多評論
              

            > hi的博客
            99久久无色码中文字幕人妻| 久久亚洲AV无码精品色午夜麻豆| 亚洲国产精品热久久| 久久精品www人人爽人人| 97久久婷婷五月综合色d啪蜜芽 | 亚洲午夜久久久久久久久久| 久久这里的只有是精品23| 日产精品久久久久久久性色| 中文精品99久久国产 | 97久久综合精品久久久综合| 精品久久久久久久中文字幕| 久久久中文字幕| 久久精品国产亚洲麻豆| 国产亚洲精品美女久久久| 亚洲国产精品无码久久| 久久国产免费| 久久精品国产一区二区电影| 国产精品国色综合久久| 久久w5ww成w人免费| 国产免费福利体检区久久| 国产成人无码精品久久久性色 | 狠狠色丁香久久综合五月| 国产精品成人99久久久久91gav| AAA级久久久精品无码片| 久久天天躁狠狠躁夜夜不卡 | 久久A级毛片免费观看| 无夜精品久久久久久| 国产亚洲美女精品久久久| 久久久国产精品| 国产69精品久久久久观看软件| 国内精品人妻无码久久久影院| 伊人久久五月天| 99久久国产亚洲高清观看2024| 91亚洲国产成人久久精品| 久久综合给久久狠狠97色| 国产午夜福利精品久久| 亚洲精品无码久久久久AV麻豆| 亚洲国产精久久久久久久| 久久亚洲日韩看片无码| 97精品伊人久久大香线蕉| 久久国产AVJUST麻豆|