一个类的行为或其算法可以在运行时更改
using UnityEngine;
using System.Collections;
namespace StrategyPatternExample2
{
public class StrategyPatternExample2 : MonoBehaviour
{
Animal sparky = new Dog();
Animal tweety = new Bird();
void Start()
{
Debug.Log("Dog: " + sparky.TryToFly());
Debug.Log("Bird: " + tweety.TryToFly());
// change behaviour of dog
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
sparky.SetFlyingAbility(new ItFlys());
Debug.Log("Dog: " + sparky.TryToFly());
Debug.Log("Bird: " + tweety.TryToFly());
}
}
}
// Using Interfaces for decoupling
// putting behaviour that varies in different classes
public interface IFly
{
string Fly();
}
// Class that holds behaviour for objects that can fly
class ItFlys : IFly
{
public string Fly()
{
return "Flying high";
}
}
// Class that holds behaviour for objects that can not fly
class CantFly : IFly
{
public string Fly()
{
return "I can't fly";
}
}
class FlyingSuperFast : IFly
{
public string Fly()
{
return "Fly super fast";
}
}
// Classes that hold an instance of the behaviours above:
public class Animal
{
// hereby adding the behaviour
// it also can change that way
public IFly flyingType;
public string TryToFly()
{
return flyingType.Fly();
}
public void SetFlyingAbility(IFly newFlyingType)
{
this.flyingType = newFlyingType;
}
}
// derived objects that vary from the base Animal:
public class Dog : Animal
{
public Dog()
{
flyingType = new CantFly();
}
}
public class Bird : Animal
{
public Bird()
{
flyingType = new ItFlys();
}
}
}
策略模式确实和状态模式有相似之处,但又不太一样,状态模式旨在根据条件进行状态切换,策略模式则是直接替换行为或者方法