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


            精品久久一区二区三区| 久久精品国产精品亚洲| 亚洲精品乱码久久久久久中文字幕 | 欧美亚洲日本久久精品| 一本色道久久综合狠狠躁篇| 久久亚洲精品无码aⅴ大香 | 72种姿势欧美久久久久大黄蕉| 久久人人爽爽爽人久久久| 久久97精品久久久久久久不卡| 欧美伊人久久大香线蕉综合69| 免费精品久久天干天干| 久久精品国产一区二区三区日韩| 久久精品国产72国产精福利| 久久久噜噜噜久久中文字幕色伊伊| 久久精品人人做人人爽电影蜜月 | 亚洲日本va午夜中文字幕久久| 亚洲精品乱码久久久久久自慰 | 久久久久久夜精品精品免费啦| 91久久精品视频| 久久精品国产网红主播| 亚洲精品国精品久久99热| 国产高潮国产高潮久久久| 精品久久久久久久国产潘金莲| 人人狠狠综合久久亚洲88| 国产美女亚洲精品久久久综合| 久久精品国产精品亚洲下载| 精品一区二区久久| 精品人妻伦九区久久AAA片69| 久久露脸国产精品| 激情久久久久久久久久| 久久99国产精品久久久| 国产精品美女久久久m| 精品多毛少妇人妻AV免费久久| 久久精品国产亚洲7777| 国产69精品久久久久99| 亚洲国产精品婷婷久久| 99久久免费国产特黄| 国产精品久久久久jk制服| 国产精品国色综合久久| 精品国产91久久久久久久| avtt天堂网久久精品|