emptysoul

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  25 Posts :: 0 Stories :: 23 Comments :: 0 Trackbacks

常用链接

留言簿(18)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

策略模式(Strategy)目的是,定义一系列算法,将每个算法封装起来,并让他们可以相互替换。策略模式让算法独立于使用它的客户而变化。 状态图为:


我们使用策略模式为多个对象封装不同的排序算法,允许客户动态改变不同的排序策略。
实现代码:
//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;
}

代码中,我们使用快速排序从小到大排序,使用冒泡排序从大到小排序,场景为SortedList,由它来决定执行哪一个排序。

最后输出为:
快速排序:3 4 7 8 10 13 15 20
冒泡排序:20 15 13 10 8 7 4 3
posted on 2009-02-21 15:54 emptysoul 阅读(702) 评论(2)  编辑 收藏 引用

Feedback

# re: 设计模式-策略模式 2009-07-27 17:43 陈海涛
完全写错了,你要把这文章拿来解释多态倒可以原谅,来说策略模式就错了!
不懂的话,就别乱写!  回复  更多评论
  

# re: 设计模式-策略模式[未登录] 2010-04-30 20:51 lymons
@陈海涛
最瞧不上你这种人,动不动就说别人说的不对,总是以自己是高手自居.
你要说觉得对方写的不对,你就把你的观点写出来,指出对方哪地方写错了.
这样才能让大家受教啊.

拜托啊.....请你回到正常人的行列来.

#我只是路过这里的路人甲,看到你的回复,实在是气不过.抱歉....  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理