strategy 策略

strategy 可以封装算法 并在运行时更改他们
相比较而言,decorator 模式更改了皮肤 而 strategy 模式改变的是内脏
strategy 模式类似于factory 模式 但factory模式在创建时改变对象 ,而 stratrgy 模式则可以自由切换

public interface IHelloStrategy
{
    string GenerateHelloString();
}

public class EnglishHelloStrategy:IHelloPrinter
{
    string GenerateHelloString()
    {
        System.Console.write("");
            }

}

public class GermanHelloStrategy : IHelloPrinter
{
    string GenerateHelloString()
    {
        System.Console.write("");
            }

}

public class HelloPrinter
{
    IHelloStrategy helloStrategy;
    public IHelloStrategy HelloStrategy
    {
        get { return helloStrategy; }
        set { helloStrategy = value; }
    }

    public void PrinterHello()
        {
            if (helloStrategy != null)
            {
                Console.WriteLine(helloStrategy.GenerateHelloString());
            }
        }
}

posted on 2005-11-01 11:48  井泉  阅读(151)  评论(0编辑  收藏  举报

导航