定义一系列算法,将每个算法都封装成类,并且使相互之间可以替换。
应用:将不同算法类对象传进去,通过对象调用各自的算法实现
策略模式的核心思想:对算法、规则进行封装,使替换和新增算法更灵活。

优点:算法可自由切换、方便拓展和新增
缺点:所有策略类都需要对外暴漏

例子:有一个person类,有年龄、体重、身高三个属性,要求对一组person对象排队,有时按年龄、有时按体重、有时按身高。。。
分析:针对此问题,我们容易想到的是,按年龄、体重、身高等分别写一个比较函数,在不同的位置,根据type调用不同函数
思路缺点:如果person有100个属性要写100个比较函数,做100次if判断type

 

class Person
    {
    public:
        string m_sName;
        int    m_nAge;
        double m_dHeight;
        double m_dWeight;
    public:
        Person(string name, int a, double b, double c):m_sName(name),m_nAge(a), m_dHeight(b),m_dWeight(c){};
    };

采用策略模式:
抽象一个比较算法的基类 ICompare

 1 class ICompare
 2     {
 3     public:
 4         virtual int comparable( Person& p1, Person& p2 ) = 0;
 5     };
 6     
 7     派生不同的比较算法:
 8     
 9     class CompareByAge : public ICompare
10     {
11         int comparable( Person& p1, Person& p2 )
12         {
13             if( p1.m_nAge < p2.m_nAge )
14                 return -1;
15             else if( p1.m_nAge > p2.m_nAge )
16                 return 1;
17             else
18                 return 0;
19         }
20     };
21     
22     class CompareByHeight : public ICompare
23     {
24         int comparable( Person& p1, Person& p2 )
25         {
26             if( p1.m_dHeight < p2.m_dHeight )
27                 return -1;
28             else if( p1.m_dHeight > p2.m_dHeight )
29                 return 1;
30             else
31                 return 0;
32         }
33     };
34     
35     class CompareByWeight : public ICompare
36     {
37         int comparable( Person& p1, Person& p2 )
38         {
39             if( p1.m_dWeight < p2.m_dWeight )
40                 return -1;
41             else if( p1.m_dWeight > p2.m_dWeight )
42                 return 1;
43             else
44                 return 0;
45         }
46     };

//person的排序类
//类中成员为比较算法基类指针
//通过构造函数,指针具体绑定哪个算法

 1  class SortPerson
 2     {
 3     public 4         ICompare* m_pSortAble;
 5     public:
 6         SortPerson( ICompare* pSortAble ):m_pSortAble( pSortAble){};
 7         
 8         //排序函数,根据成员指针中的具体算法来比较排序
 9         void Sort(Person[] personList, int n)
10         {
11             for(int i=0; i<n-1; ++i)
12             {
13                 for(int j=i+1; j<n; ++j)
14                 {
15                     if( m_pSortAble.comparable(personList[i], personList[j] ) == -1)
16                     {
17                         Person tmp = personList[i];
18                         personList[i] = personList[j];
19                         personList[j] = tmp;
20                     }
21                 }
22             }
23         }
24     };
 1 int main()
 2     {
 3         Person personList[5] = {Person("a",12, 24, 78),
 4                      Person("b",14, 24, 79),
 5                      Person("c",19, 28, 78),
 6                      Person("d",15, 24, 78),
 7                      Person("a",18, 25, 88)};
 8         
 9         //构造排序类对象,传入比较算法指针
10         SortPerson sortPerson = SortPerson( &CompareByAge() );
11         
12         //排序
13         sortPerson.Sort(personList, 5);
14     
15         return 0;
16     }

 

posted on 2021-03-29 15:49  望月又一  阅读(49)  评论(0编辑  收藏  举报