• <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)解決的目標是通過一個已有的對象來創(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: 設計模式-原型模式 2010-08-23 14:17 w8u
            這樣有意思么?
            對于main來說,為什么要這么復雜呢?
            既然他都能new windows9x,windowsXP了,還搞什么clone呢?何必這么麻煩?

            另外,clone似乎有new動作,沒有對應的delete。
              回復  更多評論
              

            久久超乳爆乳中文字幕| 无码任你躁久久久久久老妇| 久久精品国产乱子伦| 狠狠色丁香婷婷久久综合五月 | 国产人久久人人人人爽| 久久综合给合久久狠狠狠97色69| 国产Av激情久久无码天堂| 99久久综合国产精品二区| 午夜精品久久久久久久无码| 国产精品无码久久综合| 久久影视综合亚洲| 久久久久免费看成人影片| 九九热久久免费视频| 久久亚洲精品无码AV红樱桃| 久久精品成人免费国产片小草| 综合久久国产九一剧情麻豆| 一本大道久久a久久精品综合| 国产成人精品久久| 91精品国产综合久久香蕉 | 亚洲午夜久久久久久久久久| 香蕉久久一区二区不卡无毒影院| 精品久久久久久久久免费影院| 热久久这里只有精品| 久久久久99精品成人片直播| 久久综合亚洲色HEZYO社区| 国产精品成人99久久久久| 久久国产亚洲精品无码| 色婷婷久久综合中文久久蜜桃av| 久久这里只有精品视频99| 国产激情久久久久影院小草| 久久免费高清视频| 久久久久亚洲av无码专区| 久久久国产精品亚洲一区| A级毛片无码久久精品免费| 99久久综合国产精品免费| 亚洲欧美日韩精品久久亚洲区| 狠狠久久综合伊人不卡| 国产精品青草久久久久福利99| 国产成人综合久久久久久| 国产三级观看久久| 久久精品人妻一区二区三区|