结构 | |
意图 | 定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。 |
适用性 |
|
using System; abstract class Strategy
{
abstract public void DoAlgorithm();
} class FirstStrategy : Strategy
{
override public void DoAlgorithm()
{
Console.WriteLine("In first strategy");
}
} class SecondStrategy : Strategy
{
override public void DoAlgorithm()
{
Console.WriteLine("In second strategy");
}
} class Context
{
Strategy s;
public Context(Strategy strat)
{
s = strat;
} public void DoWork()
{
// some of the context's own code goes here
} public void DoStrategyWork()
{
// now we can hand off to the strategy to do some
// more work
s.DoAlgorithm();
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
FirstStrategy firstStrategy = new FirstStrategy();
Context c = new Context(firstStrategy);
c.DoWork();
c.DoStrategyWork(); return ;
}
}
策略模式