策略模式(Strategy)

来源:互联网 发布:淘宝直通车怎样收费 编辑:程序博客网 时间:2024/05/21 10:01

策略模式的定义:

  1.定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。

2.策略模式使这些算法在客户端调用他们的时候能够互不影响的变化。


策略模式的意义:

策略模式使开发人员能够开发出由许多可替换的部分组成的软件,并且各个部分之间是弱连接的关系。

弱连接的特性使软件具有更强的可扩展性,易于维护,更重要的是,它大大提高了软件的可重用性。

策略模式的组成:

抽象策略角色:策略类,通常由一个接口或者抽象类实现。

具体策略角色:包装了相关的算法和行为

环境角色:持有一个策略类的引用,最终给客户端调用的。


策略模式的实现:

策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使它们可以相互替换。

策略模式使得算法可以在不影响到客户端的情况下发生变化。使用策略模式可以把行为和环境分割开来。

环境类负责查询和维持行为类,各种算法则在具体策略中提供,由于算法和环境独立开来,算法的修改都不会影响环境和客户端。



编写策略模式的步骤

1.对策略对象定义一个公共接口。

2.编写策略类,该类实现了上面的公共接口。

3.在使用策略对象的类中保存一个对策略对象的引用。

4.在使用策略对象的类中,实现对策略对象的set和get。(注入)或者使用构造方法完成赋值。


策略模式的缺点:

1.客户端必须要知道所有的策略类,并自行决定使用哪一个策略类。

2.造成很多的策略类。


解决方案

工厂方法


实现代码:

抽象策略角色实现代码:

*********************************************************************************************************************

strategy.h 

*********************************************************************************************************************

#ifndef STRATEGY_H
#define STRATEGY_H
#include<QList>
class strategy
{
public:
    strategy();
   virtual  void  Sort(QList <int>& list);
};
#endif // STRATEGY_H

**************************************************************************************************************************

strategy.cpp

**************************************************************************************************************************

#include "strategy.h"
strategy::strategy()
{
}
void strategy::Sort(QList<int>& list)
{
}
***********************************************************************************************************************

具体策略角色实现代码(2个具体策略角色)

**********************************************************************************************************************

快速排序策略角色1  quicksort.h

**********************************************************************************************************************

#ifndef QUICKSORT_H
#define QUICKSORT_H
#include"strategy.h"
class QuickSort :public  strategy
{
public:
    QuickSort();
    void Sort(QList <int> &list);
private:
    void qSort(QList <int> &list,int low,int high);
};
#endif // QUICKSORT_H
*******************************************************************************************************************

quicksort.cpp

*******************************************************************************************************************

#include "quicksort.h"
#include<QDebug>
QuickSort::QuickSort()
{
}
void QuickSort::qSort(QList <int> &list,int low,int high)
{
    if(low>=high)
    {
        return;
    }
    int first = low;
    int last = high;
    int key = list[first];//第一个作为参考点
    while(first<last)
    {
       while(first<last&&list[last]>key)
       {
           last--;
       }
        list[first]=list[last];//将比第一个小的移动到低端
        while(first<last&&list[first]<=key)
        {
            first++;
        }
      list[last]=list[first];//将比第一个大的移动到高端
    }
  list[first]=key;//恢复参考点
  qSort(list,low,first-1);
  qSort(list,first+1,high);
}
void  QuickSort::Sort(QList <int> &list)
{
    qSort(list,0,list.count()-1);
    qDebug()<<"Quick sort";
}
*********************************************************************************************************

冒泡排序策略角色2:bubblesortstrategy.h

********************************************************************************************************

#ifndef BUBBLESORTSTRATEGY_H
#define BUBBLESORTSTRATEGY_H
#include"strategy.h"
#include<QList>
class BubbleSortStrategy :public  strategy
{
public:
    BubbleSortStrategy();
    void  Sort(QList <int>& list);
};
#endif // BUBBLESORTSTRATEGY_H
********************************************************************************************

bubblesortstrategy.cpp

********************************************************************************************

#include "bubblesortstrategy.h"
#include<QDebug>
BubbleSortStrategy::BubbleSortStrategy()
{
}
void BubbleSortStrategy::Sort(QList<int>& list)
{
    bool flag= false;
for(int i=0;i<list.count();++i)
{
    for(int j=0;j<list.count()-1-i;++j)
    {
        if(list[j]>list[j+1])
        {
            int temp = list[j];
            list[j]=list[j+1];
            list[j+1]=temp;
            flag=true;
        }
    }
    if(!flag)
    {
        break;
    }
}
    qDebug()<<"Bubble sort";
}
*************************************************************************************

环境角色 实现  context.h

*************************************************************************************

#ifndef CONTEXT_H
#define CONTEXT_H
#include<QList>
#include"strategy.h"
class Context
{
public:
    Context();
     void  setStrategy(strategy * _stragy);
     void Sort();
     void  Add(int i);
     void disPlay();
private:
    QList<int> list;
    strategy * stragy;
};
#endif // CONTEXT_H
*************************************************************************************

context.cpp

*************************************************************************************

#include "context.h"
#include<QDebug>
Context::Context()
{
}
void Context::setStrategy(strategy *_stragy)
{
    stragy=_stragy;
}
void Context::Sort()
{
    stragy->Sort(list);
}
void Context::Add(int i)
{
    list.append(i);
}
void Context::disPlay()
{
    foreach (int i, list) {
       qDebug()<<"i :"<<i;
    }
}
*******************************************************************

main 函数  客户端调用代码 

****************************************************************

#include <QCoreApplication>
#include"context.h"
#include"quicksort.h"
#include"bubblesortstrategy.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
   Context *text = new  Context;
   text->Add(1);
   text->Add(9);
   text->Add(-3);
   text->Add(-1);
    QuickSort * quick = new QuickSort;
    text->setStrategy(quick);
    text->Sort();
    text->disPlay();
    BubbleSortStrategy * bubble  = new BubbleSortStrategy;
    text->setStrategy(bubble);
    text->Sort();
    text->disPlay();
    return a.exec();
}



0 0