• <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++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(18)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

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


            實現代碼:
            //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 
            << "創建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 
            << "創建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;
            }

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

            Feedback

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

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

            久久久久亚洲精品日久生情| 久久99中文字幕久久| 18禁黄久久久AAA片| 色婷婷综合久久久久中文| 久久久久国产一级毛片高清版| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久国产色AV免费观看| 精品国产青草久久久久福利| 性欧美大战久久久久久久久| 狠狠色综合久久久久尤物| 色综合久久无码中文字幕| 久久99精品国产麻豆蜜芽| 国内精品人妻无码久久久影院| 午夜精品久久久久久影视777| 久久91精品久久91综合| 久久亚洲欧美国产精品| 国产成人综合久久精品红| 精品久久久久久无码中文字幕| 性高湖久久久久久久久| 久久人人爽人爽人人爽av| 久久精品这里热有精品| 久久99精品国产自在现线小黄鸭 | 久久成人精品视频| 亚洲精品美女久久777777| 综合久久精品色| 久久久久这里只有精品 | 久久婷婷五月综合成人D啪| 久久精品中文字幕久久| 热re99久久精品国99热| 久久亚洲精品无码VA大香大香| 久久精品国产国产精品四凭| 久久se精品一区二区影院| 精品欧美一区二区三区久久久| 精品国产91久久久久久久| 久久久久久狠狠丁香| 香港aa三级久久三级| 精品久久久久国产免费| 一本色道久久综合狠狠躁篇 | 久久精品一区二区三区中文字幕| 亚洲精品高清久久| 久久精品国产亚洲7777|