2

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.

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.

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

What I'm trying to do is:

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

and so on and so forth.

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?

3
  • Whoa- how are you implementing your buttons? Can you post a layout? If you're using the existing Button class, can you just use click listeners?
    – Nathan Fig
    Jul 12, 2011 at 15:41
  • well.. you can use long if-else statement instead ;-) You might want to use the vehicle object and some kind of intersection class where you lay vehicles on. 4-way intersection creates 4 vehicle objects or smth like this.
    – yosh
    Jul 12, 2011 at 15:42
  • @Nathan Here's the XML Layout and a screenshot of the buttons I have now.
    – intagli
    Jul 12, 2011 at 15:52

3 Answers 3

1

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.

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.

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.

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.

2
  • First of all, thank you very much for your answer. All my buttons basically do the same thing, they have to output car or truck and west/east/north/south and left/right/through. Of course, depending on which button is pressed. Should I decentralize the buttons into groups of buttons? Every 'truck' button? Or every 'west' button? Would that be useful here? Or will I inevitably have to write those 24 scenarios by hand
    – intagli
    Jul 12, 2011 at 15:57
  • Sounds like coding a MyButton class that takes the string(s) to output: new MyButton("car","Eastbound","left"), or new MyButton("car,Eastbound,left"), and you instantiate it 24 times, and the buttons themselves perform the log() with their string(s) when the button is pressed. Then, the switch is gone.
    – charley
    Jul 12, 2011 at 19:21
1

How about an array of strings where id is the index into the array. So you get teh string

Log.v("output", myoutputstrings[id]);

You just need to initialise it somehow, possibly read it from a file or database.

2
  • That's a great idea, but my int com.example.helloandroid.R.id.car_westbound_left = 2131034119 how could I use this as an index in an array? Edit: Aaaah, I think that was the part about initialising it, right?
    – intagli
    Jul 12, 2011 at 15:40
  • A map or hashmap may be what you want then.
    – Jaydee
    Jul 12, 2011 at 15:42
1

You can set the Tag property of the button (android:tag XML attribute) to "car,westbound,left" and retrieve it with the getTag() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.