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

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            裝飾模式(Decorator)目的是給對象添加職責,其結構圖如下:


            假設現在有一個圖書館,存有大量的書或影碟,相對于書或影碟,其本身沒有出借服務,現在就可以借助裝飾模式為其添加出借功能,結構圖如下:


            實現代碼:
            //Items.h
            class Items  
            {
            public:
                
            virtual ~Items();

                
            virtual void Display() = 0;
            protected:
                Items();
            };

            //Items.cpp
            #include "stdafx.h"
            #include 
            "Items.h"

            Items::Items()
            {

            }

            Items::
            ~Items()
            {

            }

            //Book.h
            #include "Items.h"
            #include 
            <string>

            class Book : public Items
            {
            public:
                Book();
                Book(std::
            stringint);
                
            virtual ~Book();

                
            void Display();
            private:
                std::
            string m_strName;
                
            int m_nNum;
            };

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

            using namespace std;

            Book::Book()
            {
                m_strName 
            = "";
                m_nNum 
            = 0;
            }

            Book::Book(
            string strName, int nNum)
            {
                m_strName 
            = strName;
                m_nNum 
            = nNum;
            }

            Book::
            ~Book()
            {

            }

            void Book::Display()
            {
                cout 
            << "書名:" << m_strName << " 數量:" << m_nNum << endl;
            }

            //Video.cpp
            #include "Items.h"
            #include 
            <string>

            class Video : public Items
            {
            public:
                Video();
                Video(std::
            stringintint);
                
            virtual ~Video();

                
            void Display();
            private:
                std::
            string m_strName;
                
            int m_nNum;
                
            int m_nTime;
            };

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

            using namespace std;

            Video::Video()
            {
                m_strName 
            = "";
                m_nNum 
            = 0;
                m_nTime 
            = 0;
            }

            Video::Video(
            string strName, int nNum, int nTime)
            {
                m_strName 
            = strName;
                m_nNum 
            = nNum;
                m_nTime 
            = nTime;
            }

            Video::
            ~Video()
            {

            }

            void Video::Display()
            {
                cout 
            << "電影名:" << m_strName << " 數量:" << m_nNum<< " 播放時間:" << m_nTime << "分鐘" << endl;
            }

            //Decorator.h
            #include "Items.h"

            class Decorator : public Items
            {
            public:
                Decorator();
                Decorator(Items
            *);
                
            virtual ~Decorator();

                
            virtual void Display();
            protected:
                Items
            * m_pItem;
            };

            //Decorator.cpp
            #include "stdafx.h"
            #include 
            "Decorator.h"

            Decorator::Decorator()
            {
                m_pItem 
            = NULL;
            }

            Decorator::Decorator(Items
            * pItem)
            {
                m_pItem 
            = pItem;
            }

            Decorator::
            ~Decorator()
            {
                
            if(m_pItem != NULL)
                {
                    delete m_pItem;
                    m_pItem 
            = NULL;
                }
            }

            void Decorator::Display()
            {
                m_pItem
            ->Display();
            }

            //Borrowable.h
            #include "Decorator.h"
            #include 
            <string>

            class Borrowable : public Decorator
            {
            public:
                Borrowable();
                Borrowable(Items
            *);
                
            virtual ~Borrowable();

                
            void SetBorrower(std::string);
                
            void Display();
                
            void BorrowItem();
            private:
                std::
            string m_strBorrower;
            };

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

            using namespace std;

            Borrowable::Borrowable()
            {

            }

            Borrowable::Borrowable(Items
            * pItem) : Decorator(pItem)
            {

            }

            Borrowable::
            ~Borrowable()
            {

            }

            void Borrowable::SetBorrower(string strBorrower)
            {
                m_strBorrower 
            = strBorrower;
            }

            void Borrowable::Display()
            {
                m_pItem
            ->Display();
                BorrowItem();
            }

            void Borrowable::BorrowItem()
            {
                cout 
            << "借給:" << m_strBorrower << endl;
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "Items.h"
            #include 
            "Book.h"
            #include 
            "Video.h"
            #include 
            "Decorator.h"
            #include 
            "Borrowable.h"

            int main(int argc, char* argv[])
            {
                Items
            * pItem = new Book("深入淺出設計模式"10);
                pItem
            ->Display();
                pItem 
            = new Video("反恐24小時"24200);
                Borrowable
            * pBorrow = new Borrowable(pItem);
                pBorrow
            ->SetBorrower("張三");
                pBorrow
            ->Display();
                
                
            return 0;
            }

            上面,我們顯示了一本書的信息,并且將一本影碟借給張三。

            最后輸出為:
            書名:深入淺出設計模式 數量:10
            電影名:反恐24小時 數量:24 播放時間:200分鐘
            借給:張三
            posted on 2009-02-10 21:31 emptysoul 閱讀(834) 評論(0)  編輯 收藏 引用
            久久婷婷五月综合97色直播| 国内精品久久久久影院日本| 精品久久久久久无码免费| 久久黄视频| 久久狠狠爱亚洲综合影院| A级毛片无码久久精品免费| 国内精品久久久久久久97牛牛| 国产精品久久久久久久久鸭| 久久久久久久久久免免费精品| 日韩欧美亚洲综合久久 | jizzjizz国产精品久久| 99久久精品免费看国产| 漂亮人妻被中出中文字幕久久| 久久香蕉超碰97国产精品| 久久国产精品波多野结衣AV| 久久亚洲国产精品成人AV秋霞 | 国产精品伊人久久伊人电影| 久久伊人精品一区二区三区 | 国产69精品久久久久观看软件| 91视频国产91久久久| 亚洲第一永久AV网站久久精品男人的天堂AV| 精品一二三区久久aaa片| 欧美精品丝袜久久久中文字幕| 久久99精品久久久久久久不卡 | 久久久精品久久久久影院| 97久久超碰国产精品2021| 久久人妻AV中文字幕| 欧美国产精品久久高清| 品成人欧美大片久久国产欧美| 国产亚洲欧美精品久久久| 日韩AV无码久久一区二区 | 色成年激情久久综合| 麻豆成人久久精品二区三区免费 | 无码人妻久久一区二区三区免费丨| 久久久免费观成人影院| 久久久久亚洲AV成人网人人软件| 久久国产免费观看精品| 久久香蕉国产线看观看99| 四虎国产永久免费久久| 久久综合中文字幕| 久久国产高清一区二区三区|