如何避免长时间的switch-case语句? [英] How to avoid long switch-case statements?

查看:206
本文介绍了如何避免长时间的switch-case语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前编码一个Android应用程序,将用于在交叉口计算流量。
在4路交叉口,应用程序将有24个按钮。

I am currently coding an Android app, which will be used to count Traffic at Intersections. At a 4-way Intersection, the app would have 24 buttons.

有4组,一组为:东行,南行,西行及北行的车辆。
这些4组被分成2组,每组3个按钮用于卡车和轿车。
那么这些2组,每组又分为车辆左转,右或经历。

There are 4 groups, one for: eastbound,southbound,westbound and northbound vehicles. Each of these 4 groups is divided into 2 groups of 3 buttons for trucks and cars. Each of these 2 groups is then divided into vehicles turning left,right or going through.

如何才能避免巨大的开关/ case语句确定哪个按钮被pressed什么时候?

How can I avoid a huge switch/case statement when determining which button was pressed?

我试图做的是:

每次按钮pressed,输出符合:
车辆类型,方向转向。

Each time a button is pressed, output a line with: Vehicle Type, Direction, Turn.

switch (id) {
    case R.id.car_westbound_left:
        Log.v("output", "car,westbound,left");
        break;
}

和等等,等等。

现在,我认为这不能写得很好code。
我可以创建一个类按钮,与属性:车辆类型,方向转,然后以某种方式使用呢?但我仍然需要按钮的ID,以确定哪个按钮是pressed?

Now, I think that this can't be well written code. Can I create a class "Button", with attributes: vehicle type, direction, turn and then somehow use this? but I still need the ID of the buttons to determine which button was pressed?

推荐答案

有许多设计从单一长开关到别的分解24例。常见的OO的办法将是某种形式的工厂集中,或使24独立演员有权力做你想要的东西。

There are many designs to decompose the 24-cases from a single long "switch" to something else. Common "OO" approaches would be to centralize in some kind of factory, or make 24 "independent actors" with the authority to do what you want.

例如,如果您创建了24个按钮,仅仅回显的它的日志报表时pressed,这将消除开关。如果您有其他的处理(除记录),那么你需要做出的战略决策,以集中的处理(在一个大的switch语句一样,因为你现在有),或分散即加工成自主行为 (如24智能按钮)拥有的背景和权力做你想做什么时,pressed。

For example, if you created 24 buttons that merely echo'd its log statement when pressed, that would eliminate the switch. If you have additional processing (beyond logging), then you need to make the strategic decision to "centralize" that processing (like in a single large switch statement, as you have now), or "decentralize" that processing into "independent actors" (like 24 "smart buttons") that have the context and authority to do what you want when pressed.

在分散的设计可以再成熟:你可能有一个单一的大一对象/按钮,用的状态的作为日志消息,而你只是实例这些实例24(但只有一个类)。即使24实例都做完全不同的事情,您可以进一步抽象出来在其他设计,比如实例24的对象/引用按钮24个不同的 MyOperation1 MyOperation2 MyOperation3 ...类的实例。

The "decentralized" design can then mature: You might have a single "DoIt" object/button, with the state being the log message, and you merely instantiate 24 of those instances (but only have a single class). Even if the 24 instances were to do completely different things, you can further abstract them out in other designs, like instantiating 24 objects/buttons that reference 24 different MyOperation1, MyOperation2, MyOperation3... class instances.

恕我直言,关键的设计决定是:你想要什么集中?如果他们都做pretty同样的事情,你想一个类,24个实例。如果他们的行为从根本上不同(你必须在每个情况 switch语句非常不同的逻辑),那么你可能受益于一个或-A-几个处理类,其可以共享一个共同的基类,然后实例24个按钮的每个引用其处理类实例

IMHO, the key design decision would be: What do you want to centralize? If they all do pretty much the same thing, you want one class, with 24 instances. If they behave fundamentally differently (you have very different logic in each of the case statements in your switch), then you may benefit from one-or-a-few processing classes, which may share a common base class, and then instantiate 24 buttons to each reference its processing class instance.

这篇关于如何避免长时间的switch-case语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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