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


            亚洲中文字幕无码久久综合网| 无码专区久久综合久中文字幕 | 午夜精品久久久内射近拍高清| 青青青国产成人久久111网站| 狠狠色丁香久久综合五月| 99久久无色码中文字幕| 一本伊大人香蕉久久网手机| 久久福利片| 久久精品一本到99热免费| 亚洲国产精品久久久天堂| 国产精品免费久久久久影院| 久久亚洲精品成人无码网站| 9久久9久久精品| 久久久久久久波多野结衣高潮| 久久精品国产亚洲av水果派 | 久久久无码精品亚洲日韩京东传媒| 久久久久国产精品人妻| 亚洲精品高清国产一久久| 久久天天躁狠狠躁夜夜网站| 色天使久久综合网天天| 91精品国产91久久| 99久久婷婷国产综合亚洲| 亚洲国产成人精品女人久久久 | 香蕉久久夜色精品国产2020 | 久久播电影网| 久久国产精品77777| 久久99这里只有精品国产| 久久久久久久综合综合狠狠| 久久精品这里热有精品| 久久精品亚洲一区二区三区浴池| 亚洲精品乱码久久久久久蜜桃 | 国产一级持黄大片99久久| 婷婷五月深深久久精品| 狠狠色丁香久久婷婷综合蜜芽五月 | 久久精品国产亚洲一区二区| 久久精品人人做人人爽97| 精品国产乱码久久久久久人妻| 久久久网中文字幕| 亚洲国产成人久久一区WWW| 久久久久九九精品影院| 一本久久免费视频|