策略模式介绍
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换
策略模式
using UnityEngine;
using System.Collections;
namespace StrategyStructure
{
public class StrategyStructure : MonoBehaviour
{
void Start ( )
{
Context context;
// Three contexts following different strategies
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
}
}
/// <summary>
/// The 'Strategy' abstract class
/// </summary>
abstract class Strategy
{
public abstract void AlgorithmInterface();
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class ConcreteStrategyA : Strategy
{
public override void AlgorithmInterface()
{
Debug.Log("Called ConcreteStrategyA.AlgorithmInterface()");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class ConcreteStrategyB : Strategy
{
public override void AlgorithmInterface()
{
Debug.Log("Called ConcreteStrategyB.AlgorithmInterface()");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class ConcreteStrategyC : Strategy
{
public override void AlgorithmInterface()
{
Debug.Log("Called ConcreteStrategyC.AlgorithmInterface()");
}
}
/// <summary>
/// The 'Context' class
/// </summary>
class Context
{
private Strategy _strategy;
// Constructor
public Context(Strategy strategy)
{
this._strategy = strategy;
}
public void ContextInterface()
{
_strategy.AlgorithmInterface();
}
}
}
策略模式案例1
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//这段真实的代码演示了Strategy模式,它以排序对象的形式封装了排序算法。
//这允许客户端动态改变排序策略,包括快速排序,Shellsort和合并排序。
namespace StrategyPatternExample1
{
public class StrategyPatternExample1 : MonoBehaviour
{
void Start()
{
// Two contexts following different strategies
SortedList studentRecords = new SortedList();
studentRecords.Add("Samual");
studentRecords.Add("Jimmy");
studentRecords.Add("Sandra");
studentRecords.Add("Vivek");
studentRecords.Add("Anna");
studentRecords.SetSortStrategy(new QuickSort());
studentRecords.Sort();
studentRecords.SetSortStrategy(new ShellSort());
studentRecords.Sort();
studentRecords.SetSortStrategy(new MergeSort());
studentRecords.Sort();
}
}
/// <summary>
/// The 'Strategy' abstract class
/// </summary>
abstract class SortStrategy
{
public abstract void Sort(List<string> list);
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class QuickSort : SortStrategy
{
public override void Sort(List<string> list)
{
list.Sort(); // Default is Quicksort
Debug.Log("-------QuickSorted list------- ");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class ShellSort : SortStrategy
{
public override void Sort(List<string> list)
{
//list.ShellSort(); not-implemented
Debug.Log("-------ShellSorted list------- ");
}
}
/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class MergeSort : SortStrategy
{
public override void Sort(List<string> list)
{
//list.MergeSort(); not-implemented
Debug.Log("-------MergeSorted list------- ");
}
}
/// <summary>
/// The 'Context' class
/// </summary>
class SortedList
{
private List<string> _list = new List<string>();
private SortStrategy _sortstrategy;
public void SetSortStrategy(SortStrategy sortstrategy)
{
this._sortstrategy = sortstrategy;
}
public void Add(string name)
{
_list.Add(name);
}
public void Sort()
{
_sortstrategy.Sort(_list);
// Iterate over list and display results
foreach (string name in _list)
{
Debug.Log(" " + name);
}
}
}
}
策略模式案例2
策略模式案例-不良代码演示
using UnityEngine;
using System.Collections;
public class Animal_BadExample
{
// bad code:
public virtual void Fly()
{
Debug.Log ("Can Fly");
}
}
public class Dog_BadExample : Animal_BadExample
{
// bad code:
public override void Fly()
{
// override
}
}
public class Cat_BadExample : Animal_BadExample
{
// bad code:
public override void Fly()
{
// override
}
}
public class Bird_BadExample : Animal_BadExample
{
public override void Fly()
{
}
}
// 记住:
// 要
//删除重复的代码
//消除允许一个类影响其他类的因素
策略模式案例-正确代码
using UnityEngine;
using System.Collections;
namespace StrategyPatternExample2
{
public class StrategyPatternExample2 : MonoBehaviour
{
void Start()
{
// create dog and bird example objects
Animal sparky = new Dog();
Animal tweety = new Bird();
Debug.Log("Dog: " + sparky.TryToFly());
Debug.Log("Bird: " + tweety.TryToFly());
// change behaviour of dog
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();
}
}
}
// 记住:
// 要
//删除重复的代码
//消除允许一个类影响其他类的因素