记录一本设计模式的书

常用设计模式有哪些?

策略设计模式


这里有一个 策略模式 C# 示例代码

cs 复制代码
using System;
using System.Collections.Generic;

namespace RefactoringGuru.DesignPatterns.Strategy.Conceptual
{
    // The Context defines the interface of interest to clients.
    class Context
    {
        // The Context maintains a reference to one of the Strategy objects. The
        // Context does not know the concrete class of a strategy. It should
        // work with all strategies via the Strategy interface.
        private IStrategy _strategy;

        public Context()
        { }

        // Usually, the Context accepts a strategy through the constructor, but
        // also provides a setter to change it at runtime.
        public Context(IStrategy strategy)
        {
            this._strategy = strategy;
        }

        // Usually, the Context allows replacing a Strategy object at runtime.
        public void SetStrategy(IStrategy strategy)
        {
            this._strategy = strategy;
        }

        // The Context delegates some work to the Strategy object instead of
        // implementing multiple versions of the algorithm on its own.
        public void DoSomeBusinessLogic()
        {
            Console.WriteLine("Context: Sorting data using the strategy (not sure how it'll do it)");
            var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });

            string resultStr = string.Empty;
            foreach (var element in result as List<string>)
            {
                resultStr += element + ",";
            }

            Console.WriteLine(resultStr);
        }
    }

    // The Strategy interface declares operations common to all supported
    // versions of some algorithm.
    //
    // The Context uses this interface to call the algorithm defined by Concrete
    // Strategies.
    public interface IStrategy
    {
        object DoAlgorithm(object data);
    }

    // Concrete Strategies implement the algorithm while following the base
    // Strategy interface. The interface makes them interchangeable in the
    // Context.
    class ConcreteStrategyA : IStrategy
    {
        public object DoAlgorithm(object data)
        {
            var list = data as List<string>;
            list.Sort();

            return list;
        }
    }

    class ConcreteStrategyB : IStrategy
    {
        public object DoAlgorithm(object data)
        {
            var list = data as List<string>;
            list.Sort();
            list.Reverse();

            return list;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // The client code picks a concrete strategy and passes it to the
            // context. The client should be aware of the differences between
            // strategies in order to make the right choice.
            var context = new Context();

            Console.WriteLine("Client: Strategy is set to normal sorting.");
            context.SetStrategy(new ConcreteStrategyA());
            context.DoSomeBusinessLogic();

            Console.WriteLine();

            Console.WriteLine("Client: Strategy is set to reverse sorting.");
            context.SetStrategy(new ConcreteStrategyB());
            context.DoSomeBusinessLogic();
        }
    }
}
Output.txt: 执行结果
XML 复制代码
Client: Strategy is set to normal sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
a,b,c,d,e

Client: Strategy is set to reverse sorting.
Context: Sorting data using the strategy (not sure how it'll do it)
e,d,c,b,a

原文链接:C# 策略模式讲解和代码示例

++仅供学习参考,如有侵权联系我删除++

相关推荐
使一颗心免于哀伤28 分钟前
《设计模式之禅》笔记摘录 - 10.装饰模式
笔记·设计模式
IT小白架构师之路12 小时前
常用设计模式系列(九)—桥接模式
设计模式·桥接模式
CHEN5_0216 小时前
设计模式——责任链模式
java·设计模式·责任链模式
前端拿破轮17 小时前
平衡二叉树的判断——怎么在O(n)的时间复杂度内实现?
前端·算法·设计模式
牛奶咖啡1317 小时前
学习设计模式《十九》——享元模式
学习·设计模式·享元模式·认识享元模式·享元模式的优缺点·何时选用享元模式·享元模式的使用示例
讨厌吃蛋黄酥18 小时前
🌟 弹窗单例模式:防止手抖党毁灭用户体验的终极方案!
前端·javascript·设计模式
前端拿破轮20 小时前
二叉树的最小深度——和最大深度一样的逻辑?
算法·设计模式·面试
未既1 天前
java设计模式 -【策略模式】
java·设计模式·策略模式
未既1 天前
java设计模式 -【装饰器模式】
java·设计模式·装饰器模式
Amagi.1 天前
Java设计模式-适配器模式
java·设计模式·适配器模式