行为型模式-策略模式(一)

文章介绍了设计模式中的策略模式,通过一个具体的代码示例展示了如何将复杂的if-else或switch语句替换为策略模式,以提高代码的可读性和可维护性。当业务场景变得复杂时,策略模式可以提供更好的灵活性和扩展性,但同时也可能增加代码复杂度。文章强调了在适当的情景下使用设计模式的重要性,以及考虑业务量来决定是否采用设计模式。
摘要由CSDN通过智能技术生成

        今天就说一说设计模式中的策略模式,从名字来讲,意思就是,对应不同的情况,就有一种解决问题的办法,不同的情况,就有不同的应对方法,这就是策略模式,非常的智能化。

也可以参考菜鸟

策略模式 | 菜鸟教程

1.下面我们看一组经常会写到的代码

尤其在业务场景非常复杂的逻辑中,经常出现大量的if语句,包括switch语句,不过switch稍微比if语句好点。

namespace StrategyPatternDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string a = Console.ReadLine().ToString();
            if (a == "a")
            {
                a = "A";
                //业务
            }
            else if (a == "b")
            {
                a = "B";
                //业务
            }
            else if (a == "c")
            {
                a = "C";
                //业务
            }
            else if (a == "d")
            {
                a = "D";
                //业务
            }
            else if (a == "e")
            {
                a = "E";
                //业务
            }
            else if (a == string.Empty)
            {
                a = "空";
                //业务
            }
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}

该代码要实现的业务就是,输入小写字母,然后输出大写字母,再处理一些业务员需求,当然也可以ToUpper方法,就可以了,我们不做讨论,主要是要采用策略模式来设计一下(当然这个也算是过度设计了)。代码简洁、有效、实现功能才是最好的代码。

2.接下来,我们使用策略模式

TypeSelect类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPatternDemo
{
    /// <summary>
    /// 业务类型,后面继续加
    /// </summary>
    public enum TypeSelect
    {
        A = 1,
        B = 2,
        C = 3,
        D = 4,
        E = 5,
        Empty = 6,
    }
}

2.StrategyPatternDemo类,定义公共接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPatternDemo
{
    /// <summary>
    /// 定义一个公开的接口
    /// </summary>
    public interface IStrategy
    {
        void Execute(string str);
    }
}

3.StrategyPatternDemo类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPatternDemo
{
    /// <summary>
    /// 增加实例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class InstanceFactory<T> where T : class
    {
        private static Dictionary<TypeSelect, T> dicCommands = new Dictionary<TypeSelect, T>();

        public T CreateInstanceByEnumName(TypeSelect taskType)
        {
            foreach (TypeSelect cd in Enum.GetValues(typeof(TypeSelect)))
            {
                if (!dicCommands.Keys.Contains(cd))
                {
                    //此处获取业务类的种类,可以使用其他方法
                    T baseCommand = Assembly.Load(typeof(T).Assembly.GetName().Name).CreateInstance((typeof(T).Namespace + ".Business.StrategyBusiness" + cd)) as T;

                    if (baseCommand != null)
                    {
                        dicCommands.Add(cd, baseCommand);
                    }
                }
            }
            return dicCommands.FirstOrDefault(c => c.Key == taskType).Value;
        }
    }
}

4.StrategyPatternDemo类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPatternDemo
{
    /// <summary>
    /// 一般写法
    /// </summary>
    internal class Context
    {
        private IStrategy _strategy;

        public Context(IStrategy strategy)
        {
            _strategy = strategy;
        }

        public void ExecuteStrategy(string str)
        {
            _strategy?.Execute(str);
        }
    }
}

5.业务类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPatternDemo.Business
{
    internal class StrategyBusinessA   : IStrategy
    {
        public void Execute(string s)
        {
            Console.WriteLine(s);
            //其他业务
        }
    }
}

6.调用

可见,调用只需要一句话

using StrategyPatternDemo.Business;

namespace StrategyPatternDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //string a = Console.ReadLine().ToString();
            //if (a == "a")
            //{
            //    a = "A";
            //    //业务
            //}
            //else if (a == "b")
            //{
            //    a = "B";
            //    //业务
            //}
            //else if (a == "c")
            //{
            //    a = "C";
            //    //业务
            //}
            //else if (a == "d")
            //{
            //    a = "D";
            //    //业务
            //}
            //else if (a == "e")
            //{
            //    a = "E";
            //    //业务
            //}
            //else if (a == string.Empty)
            //{
            //    a = "空";
            //    //业务
            //}
            //Console.WriteLine(a);
            //Console.ReadKey();


            int a = Convert.ToInt32(Console.ReadLine());
            var str = (TypeSelect)a;   //转化类型
            new Context(new  InstanceFactory<IStrategy>().CreateInstanceByEnumName(str)).ExecuteStrategy(str.ToString());  //调用

            Console.ReadKey();


        }
    }
}

7.效果

8.增加F

只需要增加2处,其他地方不变

效果

9.文件总览 

        此文,只是为了策略模式而策略模式,设计模式在日常的开发代码中,很少使用,比如以上案例,使用设计模式,就太复杂了, 根本没必要使用。但是在用到的时候,将会非常的给力,大部分都是后期业务堆起来后,才会体现出设计模式优点,当然使用了设计模式,也有缺点,一切的一切,都按照当下的业务量来衡量,是否有必要使用设计模式。

其中ConsoleApp1是另一种方式实现,效果一样

源码:

https://download.csdn.net/download/u012563853/87993185

来源:行为型模式-策略模式(一)_故里2130的博客-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130-西安找工作

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值