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

            我參與的團(tuán)隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


            假設(shè)現(xiàn)在有一個圖書館,存有大量的書或影碟,相對于書或影碟,其本身沒有出借服務(wù),現(xiàn)在就可以借助裝飾模式為其添加出借功能,結(jié)構(gòu)圖如下:


            實現(xiàn)代碼:
            //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 << " 數(shù)量:" << 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 << " 數(shù)量:" << 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("深入淺出設(shè)計模式"10);
                pItem
            ->Display();
                pItem 
            = new Video("反恐24小時"24200);
                Borrowable
            * pBorrow = new Borrowable(pItem);
                pBorrow
            ->SetBorrower("張三");
                pBorrow
            ->Display();
                
                
            return 0;
            }

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

            最后輸出為:
            書名:深入淺出設(shè)計模式 數(shù)量:10
            電影名:反恐24小時 數(shù)量:24 播放時間:200分鐘
            借給:張三
            posted on 2009-02-10 21:31 emptysoul 閱讀(837) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久99精品国产麻豆婷婷| 伊人丁香狠狠色综合久久| 久久久WWW免费人成精品| 91精品国产综合久久香蕉| 久久精品无码专区免费| 国产成人精品综合久久久久| 国产高潮国产高潮久久久| 国产精品99久久不卡| 伊人久久精品无码av一区 | 日韩va亚洲va欧美va久久| 久久乐国产综合亚洲精品| 无码人妻精品一区二区三区久久 | 亚洲精品无码久久久久sm| 久久国产精品无码HDAV| 亚洲国产成人精品女人久久久| 亚洲中文精品久久久久久不卡| 久久e热在这里只有国产中文精品99 | 亚洲午夜久久久影院| 久久国产精品偷99| 国产精品久久久久影院嫩草| 思思久久99热只有频精品66| 久久91亚洲人成电影网站| 日日躁夜夜躁狠狠久久AV| 久久这里都是精品| 久久伊人精品青青草原日本| 一级做a爰片久久毛片16| 2021精品国产综合久久| 久久精品国产99久久久| 99精品国产99久久久久久97 | 久久无码AV一区二区三区| 久久亚洲色一区二区三区| 久久精品成人影院| 久久久综合香蕉尹人综合网| 久久久久久一区国产精品| 久久久久无码精品| 色天使久久综合网天天| 色婷婷久久久SWAG精品| 99久久国产亚洲综合精品| 久久天天躁狠狠躁夜夜躁2014| 国产一区二区久久久| 亚洲中文久久精品无码|