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

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            原型模式(Prototype)解決的目標(biāo)是通過一個已有的對象來創(chuàng)建一個新對象。
            比如在使用Ghost對系統(tǒng)進行備份時,實際上備份的是操作系統(tǒng)的一個副本,而不是一個全新的操作系統(tǒng),這時操作系統(tǒng)就是原型對象,UML圖如下:


            實現(xiàn)代碼:
            //OS.h
            class OS
            {
            public:
                
            virtual ~OS();

                
            virtual OS* Clone() const = 0;
            protected:
                OS();
            };

            //OS.cpp
            #include "stdafx.h"
            #include 
            "OS.h"

            OS::OS()
            {

            }

            OS::
            ~OS()
            {

            }

            //Windows9x.h
            #include "OS.h"

            class Windows9x : public OS
            {
            public:
                Windows9x();
                Windows9x(
            const Windows9x&);
                
            virtual ~Windows9x();

                OS
            * Clone() const;
            };

            //Windows9x.cpp
            #include "stdafx.h"
            #include 
            "Windows9x.h"
            #include 
            <iostream>

            using namespace std;

            Windows9x::Windows9x()
            {
                cout 
            << "創(chuàng)建Windows9x" << endl;
            }

            Windows9x::Windows9x(
            const Windows9x& win)
            {
                cout 
            << "克隆Windows9x" << endl;
            }

            Windows9x::
            ~Windows9x()
            {

            }

            OS
            * Windows9x::Clone() const
            {
                
            return new Windows9x(*this);
            }

            //WindowsXP.h
            #include "OS.h"

            class WindowsXP : public OS
            {
            public:
                WindowsXP();
                WindowsXP(
            const WindowsXP&);
                
            virtual ~WindowsXP();

                OS
            * Clone() const;
            };

            //WindowsXP.cpp
            #include "stdafx.h"
            #include 
            "WindowsXP.h"
            #include 
            <iostream>

            using namespace std;

            WindowsXP::WindowsXP()
            {
                cout 
            << "創(chuàng)建WindowsXP" << endl;
            }

            WindowsXP::WindowsXP(
            const WindowsXP& win)
            {
                cout 
            << "克隆WindowsXP" << endl;
            }

            WindowsXP::
            ~WindowsXP()
            {

            }

            OS
            * WindowsXP::Clone() const
            {
                
            return new WindowsXP(*this);
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "OS.h"
            #include 
            "Windows9x.h"
            #include 
            "WindowsXP.h"

            int main(int argc, char* argv[])
            {
                OS
            * pOS = new Windows9x;
                pOS
            ->Clone();
                delete pOS;
                pOS 
            = new WindowsXP;
                pOS
            ->Clone();
                delete pOS;
                
            return 0;
            }

            最后輸出為:
            創(chuàng)建Windows9x
            克隆Windows9x
            創(chuàng)建WindowsXP
            克隆WindowsXP
            posted on 2009-02-09 15:52 emptysoul 閱讀(833) 評論(1)  編輯 收藏 引用

            Feedback

            # re: 設(shè)計模式-原型模式 2010-08-23 14:17 w8u
            這樣有意思么?
            對于main來說,為什么要這么復(fù)雜呢?
            既然他都能new windows9x,windowsXP了,還搞什么clone呢?何必這么麻煩?

            另外,clone似乎有new動作,沒有對應(yīng)的delete。
              回復(fù)  更多評論
              


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


            久久国产欧美日韩精品| 人妻精品久久无码区| 精品国产一区二区三区久久| 国产精品美女久久久久网| 99热成人精品免费久久| 一级a性色生活片久久无| AV无码久久久久不卡网站下载| 国产成人精品久久| 欧美久久一区二区三区| 浪潮AV色综合久久天堂| 超级碰久久免费公开视频| 少妇无套内谢久久久久| 久久久久久久久久久免费精品| 日韩AV无码久久一区二区| 久久综合狠狠色综合伊人| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久久无码精品亚洲日韩按摩| 狠狠精品干练久久久无码中文字幕 | 成人免费网站久久久| 亚洲精品无码久久久| 国产精品热久久毛片| 精品久久久噜噜噜久久久| 日日狠狠久久偷偷色综合0| 久久99国产精品一区二区| 中文字幕乱码久久午夜| 久久国产精品偷99| 国产精品成人久久久久三级午夜电影| 人妻无码精品久久亚瑟影视| 99久久精品免费看国产一区二区三区| 国产精品99精品久久免费| 思思久久精品在热线热| 日批日出水久久亚洲精品tv| 久久精品成人免费观看97| 精品久久久久久久| 久久久精品午夜免费不卡| 久久精品成人免费看| 久久超乳爆乳中文字幕| 精品久久久久久无码专区不卡| 少妇久久久久久被弄高潮| 久久成人精品视频| 99久久精品国产高清一区二区 |