• <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>

            emptysoul

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            適配器模式(Adapter)的目標是將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作,其分為類模式及對象模式,結(jié)構(gòu)圖如下:
            類模式:

            對象模式:

            假設現(xiàn)在我們的系統(tǒng)中使用了大量的圖表功能用來統(tǒng)計數(shù)據(jù),圖表都從Chart類派生,并且所有的顯示都調(diào)用Show接口,由于一些原因,我們現(xiàn)在需要使用第三提供的圖表控件(BeautyChart),它只提供了接口給我們,并且其顯示方式改變了,為了使原來的接口與新控件接口一同工作,這時就可提供一個適配器,用來將新控件的接口適配成我們所期望的原來的接口,結(jié)構(gòu)圖如下:


            實現(xiàn)代碼:

            //Chart.h
            class Chart  
            {
            public:
                
            virtual ~Chart();

                
            virtual void Show();
            protected:
                Chart();
            };

            //Chart.cpp
            #include "stdafx.h"
            #include 
            "Chart.h"
            #include 
            <iostream>

            using namespace std;

            Chart::Chart()
            {
                
            }

            Chart::
            ~Chart()
            {

            }

            void Chart::Show()
            {
                cout 
            << "使用原始方法畫圖表" << endl;
            }

            //BeautyChart.h
            class BeautyChart  
            {
            public:
                BeautyChart();
                
            virtual ~BeautyChart();

                
            void DrawChart();
            };

            //BeautyChart.cpp
            #include "stdafx.h"
            #include 
            "BeautyChart.h"
            #include 
            <iostream>

            using namespace std;

            BeautyChart::BeautyChart()
            {

            }

            BeautyChart::
            ~BeautyChart()
            {

            }

            void BeautyChart::DrawChart()
            {
                cout 
            << "使用新的控件畫圖表" << endl;
            }

            //Adapter.h
            #include "Chart.h"

            class BeautyChart;
            class Adapter : public Chart
            {
            public:
                Adapter();
                Adapter(BeautyChart
            *);
                
            virtual ~Adapter();

                
            void Show();
            private:
                BeautyChart
            * m_pBeautyChart;
            };

            //Adapter.cpp
            #include "stdafx.h"
            #include 
            "Adapter.h"
            #include 
            "BeautyChart.h"

            Adapter::Adapter()
            {
                m_pBeautyChart 
            = NULL;
            }

            Adapter::Adapter(BeautyChart
            * pBeautyChart)
            {
                m_pBeautyChart 
            = pBeautyChart;
            }

            Adapter::
            ~Adapter()
            {
                
            if(m_pBeautyChart != NULL)
                {
                    delete m_pBeautyChart;
                    m_pBeautyChart 
            = NULL;
                }
            }

            void Adapter::Show()
            {
                
            if(m_pBeautyChart != NULL)
                {
                    m_pBeautyChart
            ->DrawChart();
                }
                
            else
                {
                    Chart::Show();
                }
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Chart.h"
            #include 
            "BeautyChart.h"
            #include 
            "Adapter.h"

            int main(int argc, char* argv[])
            {
                Chart
            * pChart = new Adapter();
                pChart
            ->Show();

                pChart 
            = new Adapter(new BeautyChart);
                pChart
            ->Show();

                
            return 0;
            }

            最后輸出為:
            使用原始方法畫圖表
            使用新的控件畫圖表

            從輸出可以看出,加入適配器后,既可以用原來的方式畫圖表,又可以用新控件來工作。

            posted on 2009-02-10 15:23 emptysoul 閱讀(344) 評論(0)  編輯 收藏 引用
            国内精品久久久久久久coent| 亚洲伊人久久综合中文成人网| 久久中文骚妇内射| 无码伊人66久久大杳蕉网站谷歌| 久久婷婷国产综合精品| 草草久久久无码国产专区| 欧美与黑人午夜性猛交久久久| 精品国产乱码久久久久久呢| 久久精品国产99久久无毒不卡| 国产午夜精品理论片久久| 亚洲中文精品久久久久久不卡| 99久久99久久精品国产| 久久久久99这里有精品10| 久久精品国产第一区二区三区| 一级女性全黄久久生活片免费 | 久久亚洲中文字幕精品一区| 久久精品人人做人人爽电影| 久久久91精品国产一区二区三区| 无码人妻久久一区二区三区蜜桃| 久久免费高清视频| 久久午夜羞羞影院免费观看| 久久经典免费视频| 亚洲国产精品成人AV无码久久综合影院| 亚洲欧美日韩中文久久| 久久狠狠色狠狠色综合| 久久人做人爽一区二区三区| 久久高潮一级毛片免费| 国产精品久久国产精麻豆99网站| 成人久久免费网站| 久久久久久亚洲精品影院| 色诱久久av| 中文精品99久久国产| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久人人爽人爽人人爽av| 久久免费高清视频| 国产精品久久久久9999高清| 久久精品国产亚洲AV麻豆网站| 蜜臀av性久久久久蜜臀aⅴ| 日韩精品无码久久久久久| 亚洲国产另类久久久精品黑人| 亚洲欧美精品一区久久中文字幕|