• <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 閱讀(834) 評論(1)  編輯 收藏 引用

            Feedback

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

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

            亚洲综合伊人久久大杳蕉| 久久久久久亚洲AV无码专区| 久久九九久精品国产| 亚洲午夜无码久久久久小说| 奇米影视7777久久精品| 热re99久久精品国产99热| 国产精品日韩欧美久久综合| 国内精品久久久久影院薰衣草 | 久久免费香蕉视频| 亚洲精品白浆高清久久久久久 | 亚洲国产日韩欧美综合久久| 亚洲精品无码久久久久久| 色综合久久88色综合天天| 国产成人精品久久| 久久精品视频91| 国产三级精品久久| 久久久久久久久无码精品亚洲日韩 | 亚洲AV日韩精品久久久久久| 久久人人超碰精品CAOPOREN | 久久久精品国产| 久久久久国产一级毛片高清板| 久久精品国产99久久无毒不卡| 香港aa三级久久三级老师2021国产三级精品三级在 | 久久久女人与动物群交毛片| 久久午夜福利无码1000合集| 人人狠狠综合久久亚洲| 久久国产V一级毛多内射| 久久这里只精品国产99热| 婷婷五月深深久久精品| 久久婷婷五月综合成人D啪| 午夜精品久久久久久| 久久综合色之久久综合| 欧美大战日韩91综合一区婷婷久久青草| 亚洲AV乱码久久精品蜜桃| 亚洲狠狠婷婷综合久久久久| 久久精品国产亚洲AV影院| 99精品久久精品一区二区| 性做久久久久久久| 国产综合久久久久久鬼色| 久久99热精品| 久久www免费人成精品香蕉|