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

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            裝飾模式(Decorator)目的是給對象添加職責,其結(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 閱讀(839) 評論(0)  編輯 收藏 引用
            久久精品国产亚洲AV无码麻豆 | 久久国产色AV免费看| 中文字幕久久久久人妻| 久久久久亚洲精品天堂| 久久国产香蕉一区精品| 青青草原精品99久久精品66| 久久国产香蕉一区精品| 国产99久久精品一区二区| 国产精品无码久久综合网| 亚洲综合日韩久久成人AV| 一级做a爱片久久毛片| 成人午夜精品无码区久久| 丁香久久婷婷国产午夜视频| 亚洲午夜久久久影院| 久久中文字幕视频、最近更新| 性做久久久久久久| 亚洲精品久久久www| 99久久精品费精品国产| 99久久成人国产精品免费| 久久九九久精品国产免费直播| 99久久99久久精品国产片果冻| 国产韩国精品一区二区三区久久| 青青青青久久精品国产h久久精品五福影院1421 | 久久久不卡国产精品一区二区| AAA级久久久精品无码片| 色天使久久综合网天天| 亚洲精品国产自在久久| 看全色黄大色大片免费久久久| 一本一道久久精品综合| 国产成人99久久亚洲综合精品| 国产午夜久久影院| 久久久综合九色合综国产| 国产欧美一区二区久久| 久久精品国产一区| 亚洲国产精品婷婷久久| 国产呻吟久久久久久久92| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 东京热TOKYO综合久久精品| 久久久久人妻一区二区三区vr| 久久亚洲精品中文字幕| 国内精品久久九九国产精品|