首页 > 技术文章 > 利用Strategy Pattern(策略模式)替换switch case

hwx0000 2019-10-11 15:59 原文

参考文档:
http://blogs.microsoft.co.il/gilf/2009/11/22/applying-strategy-pattern-instead-of-using-switch-statements/

用switch case的缺点

代码臃杂,经常一个case能写很多行,代码不易阅读

每次在使用switch case语句的时候,都应该考虑,能不能使用策略模式替代switch case
使用策略模式替代switch case的适用情况

  • switch case的代码过于臃肿
  • swtich case大多时候只在一个case里跳转(具体替代原因不是很清楚,可能是浪费了时间去编译或运行其他case的代码)

策略模式,可以把不同case对应的不同的处理办法,每一个case,建立一个类,将其从原本的类中抽离出来,作为新的类。

使用策略模式的好处

  • 易于扩展和维护,因为不会在原来的switch函数里面加东西,所以不会改变原来的代码
  • 避免阅读臃肿

使用策略模式的坏处

  • 如果是很简单的switch case,用策略模式反而会使代码的可阅读性降低。
  • 写起来麻烦一点

举一个例子,把如下swtich case,转换成strategy design pattern。

class Program
{
    public enum PassengerType
    {
        Mr,
        Mrs,
        Doctor,
    }
 
    static void Main(string[] args)
    {
        PassengerType type = PassengerType.Doctor;
        switch (type)
        {
            case PassengerType.Mr:
                // do something
                break;
            case PassengerType.Mrs:
                // do something
                break;
            case PassengerType.Doctor:
                // do something
                break;
            default:
                break;
        }
    }
}

PassengerTile为一个枚举类型,要想把Case定义成不同的类,先要定义一个公共的接口(这个接口感觉类似于C++中的纯虚函数)

public interface IPassengerTypeStrategy
{
	void ImplementCase();
}

对于每一个不同的case,定义一个对应的类,实现ImplementCase接口

//Mr:先生case
public class MrPassengerTypeStrategy : IPassengerTypeStrategy
{
	public void ImplementCase()
	{
		Console.WriteLine("I'm a Mr ");
	}
}

//Mr:女士case
public class MrsPassengerTypeStrategy : IPassengerTypeStrategy
{
    public void ImplementCase()
    {
        Console.WriteLine("I'm a Mrs ");
    }

}
 
 //Doctor: 医生case
public class DoctorPassengerTypeStrategy : IPassengerTypeStrategy
{
    public void ImplementCase()
    {
       Console.WriteLine("I'm a Doctor");
    }
}

为了根据Key,查找Case,可以定义一个Dictionary:

 public class Context
{
	//定义字典:key为人员类型,对应值为公共接口IPassengerTypeStrategy
    private static Dictionary<PassengerType, IPassengerTypeStrategy> strategiesDictionary =
        new Dictionary<PassengerType, IPassengerTypeStrategy>();
        
    //初始化容器
    static Context()
    {
        strategiesDictionary.Add(PassengerType.Mr, new MrPassengerTypeStrategy());
        strategiesDictionary.Add(PassengerType.Mrs, new MrsPassengerTypeStrategy());
        strategiesDictionary.Add(PassengerType.Doctor, new DoctorPassengerTypeStrategy());
    }
    
    //定义调用case的方法
    public static void ImplementCase(PassengerType type)
    {
        strategiesDictionary[type].ImplementCase();
    }
 
}

通过这种方式,switch case 可以简写很多:

public enum PassengerType
{
    Mr,
    Mrs,
    Doctor,
}
 
class Program
{
    static void Main(string[] args)
    {
        PassengerType type = PassengerType.Doctor;
        Context.Implement(type);
    }
}

推荐阅读