设计模式之二:策略模式(Strategy)

策略模式(strategy)定义了一个算法家族,这个算法家族封装了一系列的算法,但是这些算法之间是相互可以替换的。策略模式让算法的变化和它们调用者的变化分离开来了。
UML图如下:
这里写图片描述
主要包括:

  1. Strategy:声明了一个对所有算法而言通用的接口类,下面的Contex类使用这个接口来调用一个一个具体的Stragety的算法。
  2. ConcreteStrategy:使用Strategy这个接口具体化的算法类
  3. Context:通过一个指向具体的Strategy的指针来操作这个具体的Strategy对象。

下面是根据上面的UML图编写的C++代码:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>

class Strategy
{
    public:
        virtual void algorithmInterface()=0;

};

class ConcreteStrategyA:public Strategy
{
    public:
        void algorithmInterface()
        {
            std::cout<<"ConcreteStrategyA::algorithmInterface()"<<std::endl;
        }

};

class ConcreteStrategyB:public Strategy
{
    public:
        void algorithmInterface()
        {
            std::cout<<"ConcreteStrategyB::algorithmInterface()"<<std::endl;
        }

};

class ConcreteStrategyC:public Strategy
{
    public:
        void algorithmInterface()
        {
            std::cout<<"ConcreteStrategyC::algorithmInterface()"<<std::endl;
        }

};

class Context
{
    public:
        void setStrategy(Strategy * s)
        {
            strategy=s;
        }
        void contextInterface()
        {
            strategy->algorithmInterface();
        }
    private:
        Strategy* strategy;
};


int main()
{
    Context context;
    Strategy *sa=new ConcreteStrategyA();
    Strategy *sb=new ConcreteStrategyB();
    Strategy *sc=new ConcreteStrategyC();

    context.setStrategy(sa);
    context.contextInterface();

    context.setStrategy(sb);
    context.contextInterface();

    context.setStrategy(sc);
    context.contextInterface();

    delete sa;
    delete sb;
    delete sc;
    return 0;
}

执行结果如下:
这里写图片描述

一个具体的例子:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std;
/*一个具体的策略模式的例子
 * Strategy为SortStrategy
 * ConcreteStrategy为QuickSort,ShellSort,MergeSort
 * Contex为SortedList
 * */

//所有排序类的父类
class SortStrategy
{
    public:
            virtual void sort(list<string>& lists)=0;
};


class QuickSort:public SortStrategy
{
    public:
            void sort(list<string>& lists)
            {
                std::cout<<"QuickSort:sort"<<std::endl;
            }
};

class ShellSort:public SortStrategy
{
    public:
            void sort(list<string>& lists)
            {
                std::cout<<"ShellSort:sort"<<std::endl;
            }
};

class MergeSort:public SortStrategy
{
    public:
            void sort(list<string>& lists)
            {
                std::cout<<"Merge:sort"<<std::endl;
            }
};

class SortList
{
        public:
                void sort()
                {
                    sortStrategy->sort(lists);  
                    print();
                }
                void setSortStrategy(SortStrategy * s)
                {
                    sortStrategy=s;
                }
                void add(string str)
                {
                    lists.push_back(str);
                }
                void print()
                {
                    list<string>::iterator iter;
                    for(iter=lists.begin();iter!=lists.end();iter++)
                    {
                        std::cout<<*iter<<std::endl;
                    }
                }

        private:
                list<string> lists;
                SortStrategy * sortStrategy;
};


int main()
{

    SortList sortList;
    sortList.add("allan");
    sortList.add("john");
    sortList.add("harrison");

    QuickSort *qs=new QuickSort();
    ShellSort *ss=new ShellSort();
    MergeSort *ms=new MergeSort();

    sortList.setSortStrategy(qs);
    sortList.sort();

    sortList.setSortStrategy(ss);
    sortList.sort();

    sortList.setSortStrategy(ms);
    sortList.sort();

    delete qs;
    delete ss;
    delete ms;

    return 0;
}

执行结果:(在具体的地方加上相应的排序函数即可)
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值