策略模式定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户
Interface
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StrategyPattern.Interface { public interface FlyBehavior { void fly(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StrategyPattern.Interface { public interface QuackBehavior { void quack(); } }
Behavior
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //实现了所有不会飞鸭子的动作 public class FlyNoWay:FlyBehavior { public void fly() { Console.WriteLine("I can‘t fly"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //利用火箭动力的飞行行为 public class FlyRocketPowered:FlyBehavior { public void fly() { Console.WriteLine("I‘m flying with a rocket!"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //实现了所有有翅膀鸭子的飞行动作 public class FlyWithWings:FlyBehavior { public void fly() { Console.WriteLine("I‘m flying!"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //名为呱呱叫,其实不出声 public class MuteQuack:QuackBehavior { public void quack() { Console.WriteLine("<< Silence >>"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //呱呱叫 public class Quack:QuackBehavior { public void quack() { Console.WriteLine("Quack"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern.Behavior { //名为呱呱叫,实为吱吱叫 public class Squeak:QuackBehavior { public void quack() { Console.WriteLine("Squeak"); } } }
使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Interface; namespace StrategyPattern { public abstract class Duck { protected FlyBehavior flyBehavior; protected QuackBehavior quackBehavior; public abstract void display(); public void performFly() { flyBehavior.fly(); } public void performQuack() { quackBehavior.quack(); } public void swim() { Console.WriteLine("All ducks float, even decoys!"); } public void setFlyBehavior(FlyBehavior fb) { flyBehavior = fb; } public void setQuackBehavior(QuackBehavior qb) { quackBehavior = qb; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Behavior; namespace StrategyPattern { //绿头鸭(一种野鸭) public class MallardDuck:Duck { public MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } public override void display() { Console.WriteLine("I‘m a real Mallard duck"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using StrategyPattern.Behavior; namespace StrategyPattern { //模型鸭 public class ModelDuck:Duck { public ModelDuck() { flyBehavior = new FlyNoWay(); quackBehavior = new Quack(); } public override void display() { Console.WriteLine("I‘m a model duck"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StrategyPattern { class Program { static void Main(string[] args) { Console.WriteLine("Test Mallard Duck"); Duck mallard = new MallardDuck(); mallard.performFly(); mallard.performQuack(); Console.WriteLine("Test Model Duck"); Duck model = new ModelDuck(); model.performFly(); model.setFlyBehavior(new StrategyPattern.Behavior.FlyRocketPowered()); model.performFly(); //Stop Console.ReadLine(); } } }
时间: 2024-10-17 08:32:33