处理嵌套 if then else/嵌套 switch 语句 [英] Dealing with nested if then else/nested switch statements

查看:27
本文介绍了处理嵌套 if then else/嵌套 switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何设计模式/方法/方法可以删除嵌套的 if then else 条件/switch 语句?

Are there any design patterns/methods/ways to remove nested if then else conditions/switch statements?

我记得在 Google 代码博客文章中列出了 Google 人员使用的一些方法.不过现在好像找不到了

I remember coming across some methods used by the Google folks listed in a Google code blog post. Can’t seem to find it now though

推荐答案

您想使用重构来替换使用多态类的条件.对于示例.

You want to use a refactoring that replaces a conditional using a polymorphic class. For example.

或者这里是另一个示例

本质上,理想的情况非常简单,您可以创建一个对象层次结构,并将各种行为移动到一个覆盖方法中.您仍然需要一种方法来创建正确的类,但这可以使用工厂模式来完成.

Essentially the ideal is very simple you create an object heirarchy, and move the various behaviors into an overriden method. You will still need a method to create the right class but this can be done using a factory pattern.

让我补充一点,这并不是在所有情况下都完美的解决方案.正如(对不起,我忘记了你的名字)在我的评论中指出的那样,有时这可能会很痛苦,特别是如果你必须创建一个对象模型来做到这一点.如果你有这个,这个重构会很出色:

Let me add that this is not a perfect solution in every case. As (I forgot your name sorry) pointed in my comments, some times this can be a pain especially if you have to create an object model just to do this. This refactoring excells if you have this:

function doWork(object x)
{

   if (x is a type of Apple)
   {
      x.Eat();

   } else if (x is a type of Orange)
   {
      x.Peel();
      x.Eat();
   }

}

在这里,您可以将 switch 重构为每个水果都可以处理的新方法.

Here you can refactor the switch into some new method that each fruit will handle.

正如有人指出你如何创建正确的类型以进入 doWork,有更多的方法可以解决这个问题,那么我可能会列出一些基本的方法.第一个也是最直接的(是的,与这个问题的本质背道而驰)是一个开关:

As someone pointed out how do you create the right type to go into doWork, there are more ways to solve this problem then I could probally list so some basic ways. The first and most straight forward (and yes goes against the grain of this question) is a switch:

class FruitFactory
{
   Fruit GetMeMoreFruit(typeOfFruit)
   {
         switch (typeOfFruit)
         ...
         ...
   }
}

这种方法的好处是它易于编写,并且通常是我使用的第一种方法.虽然您仍然有一个 switch 语句,它与一个代码区域隔离并且非常基本,但它返回的是一个 n 对象.如果你只有几个对象并且它们没有改变,这很有效.

The nice thing about this approach is it's easy to write, and is usually the first method I use. While you still have a switch statement its isolated to one area of code and is very basic all it returns is a n object. If you only have a couple of objects and they;re not changing this works very well.

您可以查看的其他更复杂的模式是 抽象工厂.如果您的平台支持,您还可以动态创建 Fruit.您还可以使用类似于 Provider Pattern 的内容.对我来说,这本质上意味着你配置你的对象,然后你有一个工厂,它基于配置和你给工厂的密钥动态创建正确的类.

Other more compelx patterns you can look into is an Abstract Factory. You could also dynamically create the Fruit if your platform supports it. You could also use something like the Provider Pattern. Which essentially to me means you configure your object and then you have a factory which based on the configuration and a key you give the factory creates the right class dynamically.

这篇关于处理嵌套 if then else/嵌套 switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆