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

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

问题描述

有没有任何设计模式/方法/方法来删除嵌套,如果那么其他条件/切换语句?



我记得遇到了一些使用Google方法列出的方法在Google代码博客文章中。现在似乎没有找到它,但是

解决方案

你想使用重构来替换使用多态类的条件。 示例



或另一个 example



基本上理想的是创建对象heirarchy非常简单,并将各种行为移动到一个覆盖的方法。您仍然需要一种方法来创建正确的类,但可以使用工厂模式完成。



编辑



<让我补充一点,这不是一个完美的解决方案。因为(我忘了你的名字对不起)指出了我的意见,有时候这可能是一个痛苦,特别是如果你必须创建一个对象模型来做到这一点。如果你有这个重构,这个重构效果会很好:

  function doWork(object x)
{

如果(x是Apple的一种类型)
{
x.Eat();

} else if(x是一种橙色)
{
x.Peel();
x.Eat();
}

}

这里您可以将交换机重构每个水果将处理的一些新方法。



编辑



正如有人指出,你如何创建权利要进入doWork,有更多的方法来解决这个问题,那么我可以列举一些基本的方法。第一个也是最直截了当的(和对这个问题的对应)是一个开关:

  class FruitFactory 
{
水果GetMeMoreFruit(typeOfFruit)
{
开关(typeOfFruit)
...
...
}
}

这个方法的好处是编写很容易,通常是我使用的第一种方法。虽然你仍然有一个switch语句,它隔离到一个代码区域,并且非常基本,它返回的是一个n对象。如果你只有几个对象,他们;不改变这个工作非常好。



您可以查看的其他更多compelx模式是一个。如果您的平台支持,您还可以动态创建水果。您也可以使用提供者模式。这对我来说意味着你配置你的对象,然后你有一个基于配置的工厂,一个你给工厂的钥匙动态地创建正确的类。


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

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.

Or here's another 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.

Edit

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();
   }

}

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

Edit

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)
         ...
         ...
   }
}

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.

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 else else /嵌套的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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