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

            積木

            No sub title

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            #

            轉載自:http://blog.csdn.net/sky04/article/details/6536011

            要防止因為異常產生的內存泄漏,可以使用智能指針,也可以用
            __try
            {
            }
            __finally
            {
            }
            《Windows核心編程》一書第23~25章是很好的參考資料。

            ----------------------------------------------------

            try,catch,throw:

            try包含你要防護的代碼,稱為防護塊. 防護塊如果出現異常,會自動生成異常對象并拋出.

            catch捕捉特定的異常,并在其中進行適當處理.

            throw可以直接拋出/產生異常,導致控制流程轉到catch塊.

            重要觀點: C++中異常是用對象來表示的,稱為異常對象.


            基本格式:

            try { your code; }

            catch(T1 t1) //T1可以是任意類型,int,char, CException...

            { /*T1指定了你要捕捉的異常的類型,t1指定了異常 對象的名稱,當有異常拋出,異常對象將被復制到t1 中,這樣你就可以在本處理塊中使用該對象,獲取相關 信息,進行適當處理. 處理代碼;*/}

            catch(T2* pt1)

            //上面的catch是值傳遞,這里使用指針傳遞.

            { 處理代碼; }

            catch(...)

            //...是捕捉任意類型的異常.

            { 處理代碼; }

            //其他代碼;

            /*某個catch執行完,就跳轉到這里繼續執行. 在沒有使用C++異常處理的情況下,如果在 此之前出現異常,則//這里的其他代碼不會被執行 從而造成問題.請考慮在這里放置: delete pobj1; 如果不使用用try,catch機制,內存泄漏是必然的, 因為出現問題后,執行流程無法跳轉到這里. */


            /*說明: try{}之后可以跟任意個catch塊. 發生異常后,會生成臨時的異常對象,進行一些自動處理之后,程序流程跳轉到后面的catch(),逐個檢查這些catch(),如果與catch() 中指定的類型一致,則將對象拷貝給catch參數中的對象, 接著執行該catch塊中的代碼,然后跳過其他所有剩下的catch, 繼續執行后續的代碼.
            上面所說的自動處理指的是堆棧回退,說白了就是為函數中的局部對象調用析構函數,保證這些局部對象行為良好. */


            catch()的順序通常按照:從特殊到一般的順序: catch(Tsub o){} catch(Tbase o){} catch(...){} 如果第一個catch為catch(Tbase){},則它將捕捉其所有派生類的 異常對象. 如果第一個catch為catch(...){},則其后的所有catch永遠不可能 被執行.
            重新拋出異常: 從上面的處理機制可以看到,只有一個catch可能被執行, 如果一個catch被執行,其他后續的catch就會被跳過了. 有時候一個catch中可能無法完成異常的全部處理,需要將 異常提交給更高的層,以期望得到處理.重新拋出異常實現 了這種可能性. 語法: throw ; 空的throw語句,只能在catch中使用. 它重新拋出異常對象,其外層的catch可能可以捕捉這個重新拋出的異常并做適當處理.

            ---------------------------------------------------------------------------------------------------------

            1、基礎介紹
            try
            {
            //程序中拋出異常
            throw value;
            }
            catch(valuetype v)
            {
            //例外處理程序段
            }
            語法小結:throw拋出值,catch接受,當然,throw必須在“try語句塊”中才有效。

            2、深入throw:
            (i)、程序接受到throw語句后就會自動調用析構器,把該域(try后的括號內)對象clean up,然后再進
            入catch語句(如果在循環體中就退出循環)。

            這種機制會引起一些致命的錯誤,比如,當“類”有指針成員變量時(又是指針?。?“類的構建器
            ”中的throw語句引起的退出,會導致這個指針所指向的對象沒有被析構。這里很基礎,就不深入了,提
            示一下,把指針改為類就行了,比如模板類來代替指針,在模板類的內部設置一個析構函數。

            (ii)、語句“throw;”拋出一個無法被捕獲的異常,即使是catch(...)也不能捕捉到,這時進入終止函數
            ,見下catch。

            3、深入catch:
            一般的catch出現的形式是:
            try{}
            catch(except1&){}
            catch(except2&){}
            catch(...){} //接受所有異常
            一般都寫成引用(except1&),原因很簡單,效率。

            問題a:拋出異常,但是catch不到異常怎么辦?(注意沒有java類似的finally語句)
            在catch沒有捕獲到匹配的異常的時候,會調用默認的終止函數??梢哉{用set_terminate()來設置終止函數,參數是一個函數指針,類型是:void (*terminate)()。

            到這里,可以題個問題:“沒有try-catch,直接在程序中"throw;",會怎么樣?”


            其他一些技巧:
            4、try一個函數體,形式如下
            void fun(type1,type2) try----try放在函數體后
            {
            函數定義
            }
            catch(typeX){}
            這個用法的效果就相當于:
            void fun()
            {
            try{函數定義}
            }


            5、throw一個函數體,形式如下:
            void fun (); // 能拋出任何類型的異常
            void fun () throw(except1,except2,except3)
            // 后面括號里面是一個異常參數表,本例中只能拋出這3中異常
            void fun () throw() // 參數表為空,不能拋出異常

            問題b:假設fun()中拋出了一個不在“異常參數表”中的異常,會怎么樣?

            答:調用set_terminate()中設定的終止函數。然而,這只是表面現象,實際上是調用默認的unexpected()函數,然而這個默認的 unexpected()調用了set_terminate()中設定的終止函數??梢杂胹et_unexpected()來設置unexpected, 就像set_terminate()一樣的用法,但是在設定了新的“unexpected()”之后,就不會再調用set_terminater中設定的 終止函數了。

            這個語法是很有用的,因為在用別人的代碼時,不知道哪個地方會調用什么函數又會拋出什么異常,用一個異常參數表在申明時限制一下,很實用。
            ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            try{} catch(…){}

            以前都是用try{} catch(…){}來捕獲C++中一些意想不到的異常,今天看了Winhack的帖子才知道,這種方法在VC中其實是靠不住的。例如下面的代碼:

            1. try
            2. {
            3. BYTE*pch;
            4. pch=( BYTE*)00001234; // 給予一個非法地址
            5. *pch=6;// 對非法地址賦值,會造成Access Violation 異常
            6. }
            7. catch(...)
            8. {
            9. AfxMessageBox( "catched");
            10. }

            這段代碼在debug下沒有問題,異常會被捕獲,會彈出”catched”的消息框。但在Release方式下如果選擇了編譯器代碼優化選項,則VC編譯器會去搜索try塊中的代碼, 如果沒有找到throw代碼,他就會認為try catch結構是多余的, 給優化掉。這樣造成在Release模式下,上述代碼中的異常不能被捕獲,從而迫使程序彈出錯誤提示框退出。

            那么能否在release代碼優化狀態下捕獲這個異常呢, 答案是有的。 就是__try, __except結構,上述代碼如果改成如下代碼異常即可捕獲。

            1. __try
            2. {
            3. BYTE*pch;
            4. pch=( BYTE*)00001234; // 給予一個非法地址
            5. *pch=6;// 對非法地址賦值,會造成Access Violation 異常
            6. }
            7. __except( EXCEPTION_EXECUTE_HANDLER)
            8. {
            9. AfxMessageBox( "catched");
            10. }

            但是用__try, __except塊還有問題, 就是這個不是C++標準, 而是Windows平臺特有的擴展。而且如果在使用過程中涉及局部對象析構函數的調用,則會出現C2712的編譯錯誤。 那么還有沒有別的辦法呢?

            當然有, 就是仍然使用C++標準的try{}catch(..){}, 但在編譯命令行中加入 /EHa的參數。這樣VC編譯器不會把try catch模塊給優化掉了。

            一篇比較好的英文文章談這個問題: http://members.cox.net/doug_web/eh.htm


            posted @ 2013-03-11 16:51 Jacc.Kim 閱讀(1079) | 評論 (0)編輯 收藏

            轉載自:http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html

            一、兩種機制的四種不同的事件

            CCStandardTouchDelegate 默認事件
            virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); 處理按下事件
            virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); 處理按下并移動事件
            virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); 處理松開事件
            virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent); 處理打斷事件
            CCTargetedTouchDelegate
            virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 處理用戶按下事件,true表示繼續處理, 否則false.
            virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); 處理按下并移動事件
            virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 處理松開事件
            virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); 處理打斷事件

            兩者的區別: CCSetCCTouch ,一個事件集合一個單個事件。

            事件分發的順序: CCTargetedTouchDelegateCCStandardTouchDelegate 。

            默認情況下所有 CCLayer 都沒有啟用觸摸事件,需要 this->setIsTouchEnabled(true); 啟用。

            如需更改事件: void registerWithTouchDispatcher(void) {}

            class MyLayer: public cocos2d:CCLayer {
            public:
                virtual void registerWithTouchDispatcher(void);
            
                // addStandardDelegate()
                virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
                virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent);
                virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);
                virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent);
            
                // addTargetedDelegate()
                virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
                virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
                virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
                virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
            }
            
            void MyLayer::registerWithTouchDispatcher(void) {
                // 委托,優先級
                CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, kCCMenuTouchPriority);
                // 委托,優先級,是否繼續處理
                CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority, true);
            
                // 2.0版本以后
                CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, kCCMenuHandlerPriority);
                CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
            }

            利用 ccTouchBeganccTouchesBegan 加以實現點擊的回調

            void MyLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {
                // 單點
                CCTouch *pTouch = (CCTouch*)(pTouches->anyObject());
            
                // 所有點
                for(CCSetIterator iterTouch = pTouches->begin(); iterTouch != pTouches->end(); iterTouch++) {
                    CCTouch *pCurTouch =  (CCTouch*)(*iterTouch);
                }
            
                // 獲取點在視圖中的坐標(左上角為原點)
                CCPoint touchLocation = pTouch->getLocationInView();
                // 把點的坐標轉換成OpenGL坐標(左下角為原點)
                touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
                // 把OpenGL的坐標轉換成CCLayer的坐標
                CCPoint local = convertToNodeSpace(touchLocation)
                // 大小為100x100,坐標為(0, 0)的矩形
                CCRect * rect = CCRectMake(0, 0, 100, 100);
                // 判斷該坐標是否在rect矩形內
                bool flag = rect.containsPoint(local)
                if(flag) {
                    // 回調
                } else {
                    // 不執行
                }
            }


            posted @ 2013-03-10 00:28 Jacc.Kim 閱讀(5888) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150249596/ 表示一個作用于某對象結構中的各元素的操作。它可以在不改變各元素的類的前提下定義作用于這些元素的新的操作。 “Represent an operation to be performed on the elements of an object ...  閱讀全文
            posted @ 2013-03-08 16:01 Jacc.Kim 閱讀(249) | 評論 (0)編輯 收藏

            轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150231168/

            在一個方法中定義一個算法的框架,并將該算法的某些步驟,延遲到子類實現。Template Method使得子類可以重新定義一個算法中的某些特定的步驟,而無需改變整個算法的結構。

            “Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.” - GoF

            換言之,Template Method提供一個方法,以允許子類重寫該方法的一部分,而無需重寫整個子類。

            比如,對于某一項任務,如果它有一個復雜的成員函數,并且該成員函數可以分成幾個步驟,同時這幾個步驟構成成員函數的整體結構式穩定的,但各個子步驟卻有很多改變的需求,這樣的情形下就特別適合使用Template Method。Template Method設計模式就是在確定穩定的成員函數組成結構的前提下,應對各個子步驟的變化。

            Template Method模式之UML類圖:

            25. C++實現Behavioral - Template Method模式 - 玄機逸士 - 玄機逸士博客

            業務示例:

            測試各種不同的小汽車。

            //TemplateMethod.h

            #include <iostream>

            using namespace std;

            class TestVehicle

            {

            public:

            void test() // 測試。這就是Template Method。它一共由6個步驟按照一定的時間

            { // 順序組成,但各個步驟的實現被延遲到TestVehicle的子類

            cout << "Start to test...." << endl; // 模擬固定部分的代碼

            start_up(); // 啟動

            blow_horn(); // 按喇叭

            run(); // 行駛

            turn(); // 轉彎

            brake(); // 剎車

            stop(); // 停車

            cout << "Test finished..." << endl; // 模擬固定部分的代碼

            }

            virtual ~TestVehicle()

            {

            cout << "in the destructor of TestVehicle..." << endl;

            }

            protected:

            virtual void start_up() = 0;

            virtual void blow_horn() = 0;

            virtual void run() = 0;

            virtual void turn() = 0;

            virtual void brake() = 0;

            virtual void stop() = 0;

            };

            // 測試帕沙特

            class TestPassat : public TestVehicle

            {

            public:

            ~TestPassat()

            {

            cout << "in the destructor of TestPassat..." << endl;

            }

            protected:

            void start_up()

            {

            cout << "--- Passat:\tstart up ---" << endl; // 模擬啟動Passat

            }

            void blow_horn()

            {

            cout << "--- Passat:\tblow the horn ---" << endl; // 模擬按Passat的喇叭

            }

            void run()

            {

            cout << "--- Passat:\trun ---" << endl; // 模擬Passat行駛

            }

            void turn()

            {

            cout << "--- Passat:\ttrun ---" << endl; // 模擬Passat轉彎

            }

            void brake()

            {

            cout << "--- Passat:\tbrake ---" << endl; // 模擬Passat剎車

            }

            void stop()

            {

            cout << "--- Passat:\tstop ---" << endl; // 模擬Passat停車

            }

            };

            // 測試捷達

            class TestJetta : public TestVehicle

            {

            public:

            ~TestJetta()

            {

            cout << "in the destructor of TestJetta..." << endl;

            }

            protected:

            void start_up()

            {

            cout << "--- Jetta:\tstart up ---" << endl; // 模擬按Jetta的喇叭

            }

            void blow_horn()

            {

            cout << "--- Jetta:\tblow the horn ---" << endl; // 模擬按Jetta的喇叭

            }

            void run()

            {

            cout << "--- Jetta:\trun ---" << endl; // 模擬Jetta行駛

            }

            void turn()

            {

            cout << "--- Jetta:\ttrun ---" << endl; // 模擬Jetta轉彎

            }

            void brake()

            {

            cout << "--- Jetta:\tbrake ---" << endl; // 模擬Jetta剎車

            }

            void stop()

            {

            cout << "--- Jetta:\tstop ---" << endl; // 模擬Jetta停車

            }

            };

            // TemplateMethod.cpp

            #include "TemplateMethod.h"

            int main(int argc, char** argv)

            {

            // 測試帕沙特

            TestVehicle *tvPassat = new TestPassat();

            tvPassat->test();

            cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

            // 測試捷達

            TestVehicle *tvJetta = new TestJetta();

            tvJetta->test();

            cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;

            delete tvPassat;

            delete tvJetta;

            return 0;

            }

            運行結果:

            Start to test....

            --- Passat: start up ---

            --- Passat: blow the horn ---

            --- Passat: run ---

            --- Passat: trun ---

            --- Passat: brake ---

            --- Passat: stop ---

            Test finished...

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            Start to test....

            --- Jetta: start up ---

            --- Jetta: blow the horn ---

            --- Jetta: run ---

            --- Jetta: trun ---

            --- Jetta: brake ---

            --- Jetta: stop ---

            Test finished...

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            in the destructor of TestPassat...

            in the destructor of TestVehicle...

            in the destructor of TestJetta...

            in the destructor of TestVehicle...

            上述程序的UML類圖:

            25. C++實現Behavioral - Template Method模式 - 玄機逸士 - 玄機逸士博客

            Template Method模式應該是GoF給出的23個模式中相對簡單的一個。

            posted @ 2013-03-08 15:47 Jacc.Kim 閱讀(217) | 評論 (0)編輯 收藏

            轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150224904/

            也稱為Policy模式。

            定義一系列算法,把他們一個個封裝起來,并且使他們可以相互替換。該模式使得算法可以獨立于使用它的客戶而變化。

            “Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”- GoF

            在軟件構建過程中,某些對象使用的算法可能多種多樣,經常改變,如果將這些算法都編碼到某個對象中,將會使該對象變得異常復雜;而且有時候支持不使用的算法也是一個性能負擔。Strategy設計模式就是在運行時根據需要透明地更改對象的算法,將算法和對象本身解耦。

            我們在編程中,經常碰到這樣的情況:用不同的辦法去做同一件事情,比如,

            1. 將文件保存為不同的格式;

            2. 用不同的壓縮算法壓縮一個文本文件;

            3. 用不同的壓縮算法壓縮視頻文件

            4. 將同樣的數據,用不同的圖形顯示出來:折線圖、柱狀圖或者餅圖;

            5. 將用某種語言寫的一個句子,翻譯成為幾種其他的語言。

            客戶程序告訴驅動模塊(不妨稱為Context),它將使用哪個算法(即所謂的strategypolicy),并請求Context執行該算法。

            Strategy模式UML類圖:

            24. C++實現Behavioral - Strategy模式 - 玄機逸士 - 玄機逸士博客
            v

            角色:

            Strategy

            - 給所有支持的算法聲明一個通用接口,Context使用該接口調用由ConcreteStrategy定義的算法。

            ConcreteStrategy

            - 按照Strategy給出的接口實現具體的算法。

            Context

            - 包含一個Strategy的引用;

            - 可以由ConcreteStrategy對象對其進行配置。

            業務示例:

            將阿拉伯數字“520”分別翻譯成中文、英語和俄語。

            下面是具體C++代碼:

            // Strategy.h

            #include <iostream>

            #include <string>

            #include <memory>

            using namespace std;

            // Strategy抽象類,用做借口

            class Strategy

            {

            public:

            virtual string substitute(string str) = 0;

            public:

            virtual ~Strategy()

            {

            cout << "in the destructor of Strategy..." << endl;

            }

            };

            // 中文Strategy

            class ChineseStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "我愛你");

            return tempstr;

            }

            public:

            ~ChineseStrategy()

            {

            cout << "in the destructor of ChineseStrategy..." << endl;

            }

            };

            // 英語Strategy

            class EnglishStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "I love you");

            return tempstr;

            }

            public:

            ~EnglishStrategy()

            {

            cout << "in the destructor of EnglishStrategy..." << endl;

            }

            };

            // 俄語Strategy

            class RussianStrategy : public Strategy

            {

            public:

            string substitute(string str)

            {

            size_t index = str.find("520");

            string tempstr = str.replace(index, 3, "Я люблю тебя");

            return tempstr;

            }

            public:

            ~RussianStrategy()

            {

            cout << "in the destructor of RussiaStrategy..." << endl;

            }

            };

            // Context

            class Translator

            {

            private:

            auto_ptr<Strategy> strategy;

            public:

            ~Translator()

            {

            cout << "in the destructor of Translator..." << endl;

            }

            public:

            void set_strategy(auto_ptr<Strategy> strategy)

            {

            this->strategy = strategy;

            }

            string translate(string str)

            {

            if(0 == strategy.get()) return "";

            return strategy->substitute(str);

            }

            };

            // Strategy.cpp

            #include "Strategy.h"

            int main(int argc, char** argv)

            {

            string str("321520");

            Translator* translator = new Translator;

            // 未指定strategy

            cout << "No strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成中文

            auto_ptr<Strategy> s1(new ChineseStrategy());

            translator->set_strategy(s1);

            cout << "Chinese strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成英語

            auto_ptr<Strategy> s2(new EnglishStrategy());

            translator->set_strategy(s2);

            cout << "English strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            // 翻譯成俄語

            auto_ptr<Strategy> s3(new RussianStrategy());

            translator->set_strategy(s3);

            cout << "Russian strategy: " << translator->translate(str) << endl;

            cout << "--------------------------" << endl;

            delete translator;

            return 0;

            }

            運行結果:

            No strategy:

            --------------------------

            Chinese strategy: 321我愛你

            --------------------------

            in the destructor of ChineseStrategy...

            in the destructor of Strategy...

            English strategy: 321I love you

            --------------------------

            in the destructor of EnglishStrategy...

            in the destructor of Strategy...

            Russian strategy: 321Я люблю тебя

            --------------------------

            in the destructor of Translator...

            in the destructor of RussiaStrategy...

            in the destructor of Strategy...

            結果符合預期。

            1. 從上面程序可以很容易看出,可以在運行時改變translator對象的行為;

            2. 算法如果改變了,客戶端程序不需要做任何改動,比如在EnglishStrategy的實現中將“520翻譯成“Five hundred and twenty”,客戶端代碼無需任何改變;

            3. 當需要增加新的算法時,比如要將“520”翻譯成日語的“わたし愛してるあなた”,只需要增加一個具體的Strategy類,比如JapaneseStrategy類,并重寫Strategy中聲明的純虛函數即可。這完全符合OCP。

            上述代碼的UML類圖:

            24. C++實現Behavioral - Strategy模式 - 玄機逸士 - 玄機逸士博客

            Strategy模式和State粗看起來非常相似,但他們的意圖完全不同。他們主要的區別在于:

            1. Strategy一次只能選擇一個strategy(即算法),而State模式中,不同的狀態有可能同時被激活;

            2. Strategy封裝的是算法,State封裝的是狀態;

            3. Strategy所封裝的算法(每個算法對應一個類),所做的事情相差無幾,而State所封裝的狀態(每個狀態對應一個類),往往頗不相同;

            4. State模式中的狀態遷移的概念,在Strategy中根本不存在。

            posted @ 2013-03-08 15:39 Jacc.Kim 閱讀(203) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/13583496020101502024824/ 也稱為Objects for States模式。 允許一個對象在其內部狀態改變時改變其行為,從而使對象看起來似乎修改了類。 “Allow an object to alter its behavior when its internal...  閱讀全文
            posted @ 2013-03-08 15:24 Jacc.Kim 閱讀(278) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/13583496020101501923571/ 也稱為Dependents或Publish-Subscribe模式。 定義對象間的一種一對多的依賴關系,以便當一個對象的狀態發生改變時,所有依賴于它的對象都得到通知并自動更新。 “Define a one-to-many depende...  閱讀全文
            posted @ 2013-03-08 15:12 Jacc.Kim 閱讀(253) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/13583496020101501825958/ Aka. Token 在不破壞封裝性的前提下,捕獲一個對象的內部狀態,并在該對象之外保存其狀態。這樣以后就可以將該對象恢復到原先保存的狀態。 “Without violating encapsulation, capture and ext...  閱讀全文
            posted @ 2013-03-08 14:51 Jacc.Kim 閱讀(206) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/1358349602010150177996/“Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from r...  閱讀全文
            posted @ 2013-03-08 14:33 Jacc.Kim 閱讀(186) | 評論 (0)編輯 收藏

                 摘要: 轉載自:http://patmusing.blog.163.com/blog/static/13583496020101501613155/ “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.&...  閱讀全文
            posted @ 2013-03-08 12:57 Jacc.Kim 閱讀(220) | 評論 (0)編輯 收藏

            僅列出標題
            共14頁: 1 2 3 4 5 6 7 8 9 Last 
            伊人久久精品无码av一区| 久久亚洲国产精品123区| 亚洲AV日韩精品久久久久久久| 无码国产69精品久久久久网站| 国产成人久久AV免费| 久久九九有精品国产23百花影院| 久久久久成人精品无码| 久久久亚洲裙底偷窥综合| 国产精品18久久久久久vr| 久久se精品一区精品二区国产 | 思思久久好好热精品国产| 久久伊人精品一区二区三区| 国内精品伊人久久久久| 一本大道久久东京热无码AV| 久久男人Av资源网站无码软件 | 亚洲日本va中文字幕久久| 狠狠色丁香婷综合久久| 久久亚洲精品无码aⅴ大香 | 色综合久久综合中文综合网 | 久久亚洲熟女cc98cm| 日本一区精品久久久久影院| 亚洲级αV无码毛片久久精品| 老司机午夜网站国内精品久久久久久久久 | 久久久久国产亚洲AV麻豆| 久久无码人妻一区二区三区午夜| 欧美精品丝袜久久久中文字幕 | 国产精品久久久久…| 国内精品综合久久久40p| 久久久久亚洲精品无码网址 | 性做久久久久久免费观看 | 亚洲а∨天堂久久精品| 精品久久久久久无码中文字幕| 国内精品久久久久伊人av| 久久久噜噜噜久久熟女AA片| 久久久久久久精品妇女99| 久久精品国产亚洲av麻豆蜜芽 | 亚洲欧美成人久久综合中文网| 久久精品国产黑森林| 99久久国产热无码精品免费久久久久| 久久se精品一区二区| 嫩草影院久久99|