【建立型】原型模式

來源:互聯網
上載者:User

標籤:

    原型模式主要是用原型執行個體指定建立原型對象的種類,並且通過拷貝原型建立新對象。最簡單的理解就是複製。就如cocos2d-x中的 class Clonable::clone();該模式的主要目的是可以在運行時,隨時建立出某個對象的副本,並且副本具有與本體一樣的狀態資訊。類圖參考如下:

    模式的編碼結構參考如下:

 1 namespace propotype 2 { 3     /************************************************************************** 4      * create   : (jacc.kim) [5-18-2016] 5      * summary  :  6     **************************************************************************/ 7     class IClonable 8     { 9     public:10         virtual IClonable* clone() = 0;11     };//class IClonable12 13     class ConcreteClass : public IClonable14     {15     public:16         // some code here........17         virtual IClonable* clone() override;18 19     private:20         // some member data here........21         int         m_nTheIntData;  // this is a test member data.22     };//class ConcreteClass23 24 }//namespace propotype
模式編碼結構參考

 

    該模式的優點非常明顯,可以在任何需要的時候,輕鬆輕鬆地複製出一份當前目標的副本出來,並且最最重要的是現場狀態會保持。另外,對於Client來說,同樣是解除了與具體對象子類的直接依賴關係。

    有優點,同樣也有缺點。它的缺點也是很明顯的,即為了同步現場狀態,有時候clone()行為會比較複雜,實現比較繁瑣。尤其在如c++這樣的語言中,必需要要注意對象間的深、淺拷貝問題,一不小心就可能出現問題。

【建立型】原型模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.