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

            常用鏈接

            留言簿(18)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

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


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


            實(shí)現(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 
            << "書(shū)名:" << 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<< " 播放時(shí)間:" << 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è)計(jì)模式"10);
                pItem
            ->Display();
                pItem 
            = new Video("反恐24小時(shí)"24200);
                Borrowable
            * pBorrow = new Borrowable(pItem);
                pBorrow
            ->SetBorrower("張三");
                pBorrow
            ->Display();
                
                
            return 0;
            }

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

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

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


            久久精品国产一区二区三区| 亚洲国产精品高清久久久| 久久精品视频网| 国产午夜福利精品久久| 无码8090精品久久一区| 亚洲午夜无码久久久久小说| 久久精品国产免费观看三人同眠| 中文国产成人精品久久不卡| 久久99精品国产麻豆宅宅| 噜噜噜色噜噜噜久久| 久久久久亚洲精品无码蜜桃| 免费观看久久精彩视频| 狠狠色丁香久久婷婷综合蜜芽五月| 伊人久久精品无码av一区| 久久久国产精华液| 久久久久久国产精品无码超碰| 青青热久久国产久精品| 国内精品九九久久久精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 性欧美丰满熟妇XXXX性久久久| 狠狠色丁香久久综合婷婷| 精品久久人人爽天天玩人人妻| 国产精品久久久久久久久久免费| 久久精品亚洲AV久久久无码| 久久久久亚洲AV无码专区网站| 亚洲午夜精品久久久久久人妖| 亚洲AV日韩AV天堂久久| 久久天天躁狠狠躁夜夜躁2014| 久久久久久久久久免免费精品| 一级做a爰片久久毛片人呢| 国产精品9999久久久久| 久久久久人妻精品一区 | 囯产极品美女高潮无套久久久| 久久精品视屏| 国产精品嫩草影院久久| 一级做a爰片久久毛片16| 热久久这里只有精品| 亚洲一区中文字幕久久| 国产呻吟久久久久久久92| 国产成人久久精品麻豆一区| 99久久亚洲综合精品网站|