• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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

            對象模式:

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


            實現代碼:

            //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 閱讀(346) 評論(0)  編輯 收藏 引用
            亚洲国产精品综合久久网络| 69久久夜色精品国产69| 日日狠狠久久偷偷色综合免费| 无码人妻少妇久久中文字幕| 久久天天躁狠狠躁夜夜不卡 | 久久久久se色偷偷亚洲精品av| 2021国产精品午夜久久| 99国产欧美久久久精品蜜芽| 日日狠狠久久偷偷色综合96蜜桃| 久久精品99久久香蕉国产色戒| 性高湖久久久久久久久| 91精品国产综合久久四虎久久无码一级 | 色婷婷综合久久久久中文| 久久综合九色综合久99| 亚州日韩精品专区久久久| 国产精品美女久久久m| 国产免费久久精品丫丫| 精品国产乱码久久久久久1区2区| 综合久久一区二区三区| 一本大道加勒比久久综合| 蜜臀久久99精品久久久久久小说| 久久人人超碰精品CAOPOREN| 欧美精品一区二区精品久久| 无码国产69精品久久久久网站| 综合久久一区二区三区 | 久久久噜噜噜久久中文字幕色伊伊| 久久精品国产亚洲一区二区| 无码久久精品国产亚洲Av影片 | 久久国产精品无码一区二区三区| 欧美久久久久久精选9999| 久久影院久久香蕉国产线看观看| 久久精品国产99国产精品亚洲| 久久久久久亚洲精品不卡| 996久久国产精品线观看| 久久永久免费人妻精品下载| 国产成人精品综合久久久| 无码人妻久久一区二区三区蜜桃| 2021最新久久久视精品爱| 无码人妻精品一区二区三区久久 | 97久久超碰国产精品2021| 无码伊人66久久大杳蕉网站谷歌|