深入浅出设计模式(C#版)--策略模式

总则:设计模式的意义是为你提供智慧,并支持你的设计过程,而不是取而代之。

 

策略模式(Strategy Pattern)

定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

以下内容高手请跳过,会浪费您时间的。

代码下载:http://download.csdn.net/detail/josslin025/6879621 (资源分0,绝不欺诈)

解压后DuckTest文件夹根目录下的 DuckTest.sln 是VS2012项目,Backup文件夹里是VS2008项目

 

上面的定义理论性强了点,下面从代码切入:

Duck抽象类:鸭子有游泳、叫、飞的行为

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

   publicabstractclassDuck

    {

      protectedFlyBehavior flyBehavior;//为接口类型声明引用变量,在派生类中再实例化

      protectedQuackBehavior quackBehavior;//为接口类型声明引用变量,在派生类中再实例化

       public Duck()

       { }

       publicvoid performFly()

       {

           flyBehavior.fly();

       }

       publicvoid performQuack()

       {

           quackBehavior.quack();

       }

       publicvoid swim()

       {

           Console.WriteLine("所有的都会游泳");

       }

       publicvoid setFlyBehavior(FlyBehavior fb)

       {

           flyBehavior = fb;

        }

       publicvoid setQuackBehavior(QuackBehavior qb)

       {

           quackBehavior = qb;

       }

    }

}

 

FlyBehavior 接口和实现该接口的类:有的鸭子不能飞,模型鸭;玩具鸭子被改造了,装上了推动装置(推动火箭),能飞了。

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

    public interfaceFlyBehavior

    {

         void fly();

    }

    publicclassFlyWithWings : FlyBehavior

    {

        publicvoid fly()

        {

            Console.WriteLine("我能飞...");

        }

 

   

    publicclassFlyNoWay : FlyBehavior

        {

            publicvoid fly()

            {

                Console.WriteLine("我不能飞...");

            }

        }

    publicclassFlyRecketPowered : FlyBehavior

    {

        publicvoid fly()

        {

            Console.WriteLine("我是火箭,当然能飞...");

        }

    }

}

 

QuackBehavior接口和实现该接口的类:动物的鸭子会嘎嘎叫;模型鸭不会叫;按压玩具鸭,它的发生装置会发出“唧唧”的声音。

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

    publicinterfaceQuackBehavior

    {

         void quack();

    }

    publicclassQuack : QuackBehavior

    {

        publicvoid quack()

        {

            Console.WriteLine("嘎嘎叫...");

        }

    }

    publicclassMuteQuack : QuackBehavior

    {

        publicvoid quack()

        {

            Console.WriteLine("不会叫...");

        }

    }

    publicclassSquack : QuackBehavior

    {

        publicvoid quack()

        {

            Console.WriteLine("唧唧叫...");

        }

    }

}

MallardDuck类:具体的类,野鸭子

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

   public classMallardDuck:Duck

    {

       public MallardDuck()

       {

            quackBehavior =newQuack();

            flyBehavior = newFlyWithWings();

       }

       publicvoid display()

       {

           Console.WriteLine("是只真正的野鸭子");

       }

    }

}

ModelDuck类:具体的类,模型鸭

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

    public classModelDuck:Duck

    {

        public ModelDuck()

        {

            flyBehavior = newFlyNoWay();

            quackBehavior =newMuteQuack();

        }

        publicvoid display()

        {

            Console.WriteLine("是模型,不能飞。。。");

        }

    }

}

测试代码

using System;

using System.Collections.Generic;

using System.Text;

 

namespace ConsoleApplicationStaticClass

{

    classMiniDuckSimulator

    {   

           publicstaticvoid Main(string[] args)

           {

               Duck mallard =newMallardDuck();

               mallard.performFly();

               mallard.performQuack();

               Duck model =newModelDuck();

               model.performQuack();

               model.performFly();

               model.setFlyBehavior(newFlyRecketPowered());

               model.performFly();

               Console.ReadKey();

           }

       }       

}

 

关键的设计在于对鸭子行为(飞和叫的多种可能性)的封装。

亲,看明白了吗?

不明白的话,可以自己下载以上代码研究(0分的资源分哦)

下载地址:http://download.csdn.net/detail/josslin025/6879621



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值