定义一系列算法,把它们一个个封装起来,并且使它们可以互相替换.该模式使得算法可以独立于使用它的客户而变化 .
----------<<设计模式>>GoF
“Strategy模式”的出现,是为了提供一套相互之间可灵活替换的算法,在不影响上层接口的情况下,用户可以自由选择不同的算法完成逻辑。
注意:这里的算法的替换是完全的算法的替换,也就是说对每一个封装的算法都可以独立去完成客户的任务,而不需要其他算法的协助.
在.NET Framwork中的ArrayList对象组合了IComparer,其实IComparer就是和Strategy.
我们可以根据需要自定义个排序的类,此类只需实现IComparer即可,这样客户代码就可以进行各个排序算法的相互切换.
using
System;
using System.Collections ;
namespace StrategyArrayListSort
... {
/**//// <summary>
/// ComparePerson 的摘要说明。
/// </summary>
public class ComparePersonCredit:IComparer
...{
public ComparePersonCredit()
...{
}
IComparer 成员#region IComparer 成员
public int Compare(object x, object y)
...{
// TODO: 添加 ComparePerson.Compare 实现
Person p1= (Person)x;
Person p2=(Person)y;
if(p1.GetCredit()>p2.GetCredit() )
return 1;
else if(p1.GetCredit()<p2.GetCredit() )
return -1;
else
return 0;
}
#endregion
}
}
using System.Collections ;
namespace StrategyArrayListSort
... {
/**//// <summary>
/// ComparePerson 的摘要说明。
/// </summary>
public class ComparePersonCredit:IComparer
...{
public ComparePersonCredit()
...{
}
IComparer 成员#region IComparer 成员
public int Compare(object x, object y)
...{
// TODO: 添加 ComparePerson.Compare 实现
Person p1= (Person)x;
Person p2=(Person)y;
if(p1.GetCredit()>p2.GetCredit() )
return 1;
else if(p1.GetCredit()<p2.GetCredit() )
return -1;
else
return 0;
}
#endregion
}
}
using
System;
namespace StrategyArrayListSort
... {
/**//// <summary>
/// Person 的摘要说明。
/// </summary>
public class Person
...{
private int credit;
private int age;
private string name;
private int account=0;
public Person(int credit,int age ,string name)
...{
this.credit = credit;
this.age =age;
this.name =name;
}
public int GetCredit()
...{
return credit;
}
public string GetName()
...{
return name;
}
public int GetAge()
...{
return age;
}
public void LoanMoney()
...{
if (credit >=100)
account+= 10000;
}
}
}
namespace StrategyArrayListSort
... {
/**//// <summary>
/// Person 的摘要说明。
/// </summary>
public class Person
...{
private int credit;
private int age;
private string name;
private int account=0;
public Person(int credit,int age ,string name)
...{
this.credit = credit;
this.age =age;
this.name =name;
}
public int GetCredit()
...{
return credit;
}
public string GetName()
...{
return name;
}
public int GetAge()
...{
return age;
}
public void LoanMoney()
...{
if (credit >=100)
account+= 10000;
}
}
}
using
System;
using System.Collections ;
namespace StrategyArrayListSort
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class MainApp
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
ArrayList personList = new ArrayList();
Person liu = new Person(100,23,"liu");
Person yu = new Person(120,24,"yu");
Person ma = new Person(90,22,"ma");
Person su = new Person(100,25,"su");
personList.Add(liu);
personList.Add(yu);
personList.Add(ma);
personList.Add(su);
personList.Sort(new ComparePersonCredit());
//personList.Sort(new ComparePersonAge());
foreach ( Person person in personList)
...{
Console.WriteLine(person.GetName());
}
Console.ReadLine();
}
}
}
using System.Collections ;
namespace StrategyArrayListSort
... {
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class MainApp
...{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
...{
ArrayList personList = new ArrayList();
Person liu = new Person(100,23,"liu");
Person yu = new Person(120,24,"yu");
Person ma = new Person(90,22,"ma");
Person su = new Person(100,25,"su");
personList.Add(liu);
personList.Add(yu);
personList.Add(ma);
personList.Add(su);
personList.Sort(new ComparePersonCredit());
//personList.Sort(new ComparePersonAge());
foreach ( Person person in personList)
...{
Console.WriteLine(person.GetName());
}
Console.ReadLine();
}
}
}
由上代码可以看出,我们还可以去定义个类ComparePersonAge来比较Person,客户代码可以很方便的去替换掉原来的排序算法ComparePersonCredit.
Strategy模式的几个要点:
Strategy及其子类为组件提供了一系列可重用的算法,从而可以使得类型在运行时方便地根据需要在各个算法之间进行切换,所谓算法的封装,支持算法的变化.
Strategy模式消除了条件判断语句,也就是在解耦合.
一般含有条件判断的地方就需要Strategy模式去解决变化.