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

            我參與的團(tuán)隊(duì)

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            策略模式(Strategy)目的是,定義一系列算法,將每個(gè)算法封裝起來,并讓他們可以相互替換。策略模式讓算法獨(dú)立于使用它的客戶而變化。 狀態(tài)圖為:


            我們使用策略模式為多個(gè)對(duì)象封裝不同的排序算法,允許客戶動(dòng)態(tài)改變不同的排序策略。
            實(shí)現(xiàn)代碼:
            //SortStrategy.h
            class SortStrategy
            {
            public:
                SortStrategy();
                
            virtual ~SortStrategy();

                
            virtual void Sort(int* pArray, int nSize) = 0;
            };

            //SortStrategy.cpp
            #include "stdafx.h"
            #include 
            "SortStrategy.h"

            SortStrategy::SortStrategy()
            {

            }

            SortStrategy::
            ~SortStrategy()
            {

            }

            //QuickSort.h
            #include "SortStrategy.h"

            class QuickSort : public SortStrategy
            {
            public:
                QuickSort();
                
            virtual ~QuickSort();

                
            void Sort(int* pArray, int nSize);
            private:
                
            void Sort(int* pArray, int nLow, int nHigh);
                
            void Swap(int& nA, int& nB);
                
            int Partition(int* pArray, int nLow, int nHigh);
            };

            //QuickSort.cpp
            #include "stdafx.h"
            #include 
            "QuickSort.h"

            QuickSort::QuickSort()
            {

            }

            QuickSort::
            ~QuickSort()
            {
                
            }

            void QuickSort::Sort(int* pArray, int nSize)
            {
                Sort(pArray, 
            0, nSize);
            }

            void QuickSort::Sort(int* pArray, int nLow, int nHigh)
            {
                
            int i;
                
            if(nLow < nHigh)
                {
                    i 
            = Partition(pArray, nLow, nHigh);
                    Sort(pArray, 
            0, i - 1);
                    Sort(pArray, i 
            + 1, nHigh);
                }
            }

            void QuickSort::Swap(int& nA, int& nB)
            {
                
            int x;
                x 
            = nA;
                nA 
            = nB;
                nB 
            = x;
            }

            int QuickSort::Partition(int* pArray, int nLow, int nHigh)
            {
                
            int i = nLow - 1;
                
            int j;
                
            for(j = nLow; j < nHigh; ++j)
                {
                    
            if(pArray[j] < pArray[nHigh])
                    {
                        Swap(pArray[
            ++i], pArray[j]);
                    }
                }
                Swap(pArray[i 
            + 1], pArray[nHigh]);

                
            return i + 1;
            }

            //BubbleSort.h
            #include "SortStrategy.h"

            class BubbleSort : public SortStrategy
            {
            public:
                BubbleSort();
                
            virtual ~BubbleSort();

                
            void Sort(int*int nSize);
            private:
                
            void Swap(int& nA, int& nB);
            };

            //BubbleSort.cpp
            #include "stdafx.h"
            #include 
            "BubbleSort.h"

            BubbleSort::BubbleSort()
            {

            }

            BubbleSort::
            ~BubbleSort()
            {

            }

            void BubbleSort::Sort(int* pArray, int nSize)
            {
                
            for (int i = nSize - 1; i >= 0--i)
                {
                    
            for(int j = 0; j <= i; ++j)
                    {
                        
            if(pArray[i] > pArray[j])
                        {
                            Swap(pArray[i], pArray[j]);
                        }
                    }
                }
            }

            void BubbleSort::Swap(int& nA, int& nB)
            {
                
            int x;
                x 
            = nA;
                nA 
            = nB;
                nB 
            = x;
            }

            //SortedList.h
            class SortStrategy;
            class SortedList  
            {
            public:
                SortedList();
                
            virtual ~SortedList();

                
            void SetSortStrategy(SortStrategy*);
                
            void Sort(int*int nSize);
                
            void Display();
            private:
                SortStrategy
            * m_pStrategy;
                
            int* m_pArray;
                
            int m_nSize;
            };

            //SortedList.cpp
            #include "stdafx.h"
            #include 
            "SortedList.h"
            #include 
            "SortStrategy.h"
            #include 
            <iostream>

            using namespace std;

            SortedList::SortedList()
            {

            }

            SortedList::
            ~SortedList()
            {
                
            if(m_pStrategy != NULL)
                {
                    delete m_pStrategy;
                    m_pStrategy 
            = NULL;
                }
            }

            void SortedList::SetSortStrategy(SortStrategy* pStrategy)
            {
                m_pStrategy 
            = pStrategy;
            }

            void SortedList::Sort(int* nArray, int nSize)
            {
                m_pArray 
            = nArray;
                m_nSize 
            = nSize;
                m_pStrategy
            ->Sort(m_pArray, nSize);
            }

            void SortedList::Display()
            {
                
            for(int i = 0; i < m_nSize; ++i)
                {
                    cout 
            << m_pArray[i] << " ";
                }
                cout 
            << endl;
            }

            //main.cpp
            #include "stdafx.h"
            #include 
            "SortedList.h"
            #include 
            "QuickSort.h"
            #include 
            "BubbleSort.h"

            int main(int argc, char* argv[])
            {
                
            int nArray[] = {871015432013};
                
            int nSize = sizeof nArray / sizeof nArray[0];
                SortedList sort;
                SortStrategy
            * pStrategy = new QuickSort;
                sort.SetSortStrategy(pStrategy);
                sort.Sort(nArray, nSize);
                printf(
            "快速排序:");
                sort.Display();

                pStrategy 
            = new BubbleSort;
                sort.SetSortStrategy(pStrategy);
                sort.Sort(nArray, nSize);
                printf(
            "冒泡排序:");
                sort.Display();

                
            return 0;
            }

            代碼中,我們使用快速排序從小到大排序,使用冒泡排序從大到小排序,場(chǎng)景為SortedList,由它來決定執(zhí)行哪一個(gè)排序。

            最后輸出為:
            快速排序:3 4 7 8 10 13 15 20
            冒泡排序:20 15 13 10 8 7 4 3
            posted on 2009-02-21 15:54 emptysoul 閱讀(735) 評(píng)論(2)  編輯 收藏 引用

            Feedback

            # re: 設(shè)計(jì)模式-策略模式 2009-07-27 17:43 陳海濤
            完全寫錯(cuò)了,你要把這文章拿來解釋多態(tài)倒可以原諒,來說策略模式就錯(cuò)了!
            不懂的話,就別亂寫!  回復(fù)  更多評(píng)論
              

            # re: 設(shè)計(jì)模式-策略模式[未登錄] 2010-04-30 20:51 lymons
            @陳海濤
            最瞧不上你這種人,動(dòng)不動(dòng)就說別人說的不對(duì),總是以自己是高手自居.
            你要說覺得對(duì)方寫的不對(duì),你就把你的觀點(diǎn)寫出來,指出對(duì)方哪地方寫錯(cuò)了.
            這樣才能讓大家受教啊.

            拜托啊.....請(qǐng)你回到正常人的行列來.

            #我只是路過這里的路人甲,看到你的回復(fù),實(shí)在是氣不過.抱歉....  回復(fù)  更多評(píng)論
              


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            无码八A片人妻少妇久久| 亚洲天堂久久久| 久久久久久精品成人免费图片| 中文字幕亚洲综合久久2| 久久综合综合久久97色| 久久精品国产99国产电影网| 精品国际久久久久999波多野| 久久这里只有精品18| 久久久无码一区二区三区| 久久99国产综合精品免费| 国产精品久久久久9999| 亚洲天堂久久精品| 久久中文精品无码中文字幕| 伊人久久大香线蕉综合热线| 亚洲精品乱码久久久久久按摩| 亚洲中文字幕无码久久精品1| 久久久噜噜噜久久中文福利| 久久精品无码一区二区三区| 国产福利电影一区二区三区久久久久成人精品综合 | 久久久国产打桩机| 午夜天堂精品久久久久| 国产精品久久久久无码av| 99国内精品久久久久久久| 久久久久国产一区二区三区| 亚洲国产日韩综合久久精品| 久久夜色精品国产噜噜麻豆 | 久久无码专区国产精品发布| 久久亚洲中文字幕精品有坂深雪 | 久久99久久99精品免视看动漫| 久久精品国产亚洲沈樵| 久久久无码精品亚洲日韩软件| 中文字幕乱码人妻无码久久| 亚洲欧美日韩精品久久| 精品综合久久久久久98| 亚洲国产精品热久久| 久久精品国产日本波多野结衣 | 久久国产精品免费| 久久久久久亚洲Av无码精品专口| 久久99精品久久久久久野外| 久久精品国产亚洲77777| 国产叼嘿久久精品久久|