Unity实现设计模式——策略模式

Unity实现设计模式------策略模式

策略模式是一种定义一些列算法的方法,这些所有的算法都是完成相同的工作,只是实现不同。它可以通过相同的方式调用所有的算法,减少各种算法类与使用算法类之间的耦合。

策略模式的 Strategy 类层次为 Context 定义了一系列的可供重用的算法或行为。继承有助于析取出这些算法中的公共功能。

策略模式就是用来封装算法的,但在实践中,我们发现可以用它来封装几乎任何类型的规则,只要在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性。

需要注意的是策略模式与工厂模式的区别

简单工厂模式下,产品类(父类)里包含了产品的属性,所以每个具体产品(子类)也就包含了同样的属性。

策略模式下,策略类(父类)里只提供了虚方法,并没有包含属性,所以每个具体的算法有自己独立的属性。

下面使用对于一个结构进行排序的例子来演示

1.SortStrategy(The 'Strategy' abstract class)

csharp 复制代码
    abstract class SortStrategy
    {
        public abstract void Sort(List<string> list);
    }

2.派生类

(1)QuickSort

csharp 复制代码
    class QuickSort : SortStrategy
    {
        public override void Sort(List<string> list)
        {
            list.Sort(); // Default is Quicksort
            Debug.Log("-------QuickSorted list------- ");
        }
    }

(2)ShellSort

csharp 复制代码
    class ShellSort : SortStrategy
    {
        public override void Sort(List<string> list)
        {
            //list.ShellSort(); not-implemented
            Debug.Log("-------ShellSorted list------- ");
        }
    }

(3)MergeSort

csharp 复制代码
    class MergeSort : SortStrategy
    {
        public override void Sort(List<string> list)
        {
            //list.MergeSort(); not-implemented
            Debug.Log("-------MergeSorted list------- ");
        }
    }

3.SortedList(The 'Context' class)

csharp 复制代码
    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);
            }
        }
    }

4.测试

csharp 复制代码
    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();

        }
    }
相关推荐
幂简集成explinks9 小时前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源
SmalBox12 小时前
【光照】Unity中的[经验模型]
unity·渲染
大飞pkz13 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
努力也学不会java13 小时前
【设计模式】抽象工厂模式
java·设计模式·oracle·抽象工厂模式
萘柰奈13 小时前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
Yasin Chen14 小时前
Unity UI坐标说明
ui·unity
陈言必行19 小时前
Unity 性能优化 之 编辑器创建资源优化( 工作流 | 场景 | 预制体)
unity·编辑器·游戏引擎
Magnetic_h1 天前
【iOS】设计模式复习
笔记·学习·ios·设计模式·objective-c·cocoa