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


            无码伊人66久久大杳蕉网站谷歌| 久久久精品2019免费观看| 99久久精品九九亚洲精品| 久久狠狠一本精品综合网| 思思久久99热只有频精品66| 久久久久亚洲AV无码网站| 久久香蕉综合色一综合色88| 日本欧美国产精品第一页久久| 国产精品久久婷婷六月丁香| 久久精品免费观看| 久久天天躁狠狠躁夜夜avapp| 97久久精品午夜一区二区| 久久免费99精品国产自在现线| 亚洲国产视频久久| 中文字幕久久欲求不满| 久久午夜夜伦鲁鲁片免费无码影视| 国产精品久久永久免费| 亚洲中文久久精品无码| 久久综合日本熟妇| 久久免费美女视频| 午夜精品久久久久久中宇| 亚洲国产精品成人久久蜜臀 | 久久综合给久久狠狠97色| 久久这里只有精品首页| 99久久久国产精品免费无卡顿| 亚洲精品乱码久久久久久不卡| 2020最新久久久视精品爱| 少妇精品久久久一区二区三区 | 国产成人精品久久免费动漫| 亚洲国产成人精品无码久久久久久综合| 狠狠色丁香久久婷婷综合五月| 亚洲精品国精品久久99热一| 亚洲性久久久影院| 色综合久久久久综合99| 很黄很污的网站久久mimi色| 亚洲国产天堂久久综合网站| 久久福利青草精品资源站| 久久久精品免费国产四虎| 精品一区二区久久久久久久网站| 国产精品久久久久久福利漫画| 久久精品人人做人人妻人人玩|