How to avoid long switch statements?

I am currently encoding an Android application that will be used to count traffic at intersections. At a 4-way intersection, the app will have 24 buttons.

There are 4 groups: one for: eastern, southern, western and northern vehicles. Each of these 4 groups is divided into 2 groups of 3 buttons for trucks and cars. Each of these two groups is then divided into vehicles turning left, right, or through.

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

What I'm trying to do is:

Each time the button is pressed, a line is displayed with: Vehicle type, direction, turn.

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

etc. etc.

Now I think this may not be well written code. Can I create a class "Button", with attributes: vehicle type, direction, turn, and then somehow use this? but do I still need a button id to determine which button was pressed?

+2
source share
3 answers

There are many projects to decompose 24 cases from one long " switch " into something else. The usual “OO” approaches would be to centralize in some kind of factory or create 24 “independent members” with the authority to do what you want.

For example, if you created 24 buttons that simply responded to its log statement when pressed, that would remove the switch. If you have additional processing (besides logging), you need to make a strategic decision to "centralize" this processing (for example, in one large switch statement, as you are now), or to "decentralize" this processing to "independent actors", ( for example, 24 “smart buttons”) that have the context and the authority to do what you want when pressed.

Then you can create a "decentralized" project: you can have one object / button "DoIt", and the state is a log message, and you simply create an instance of 24 such instances (but you have only one class). Even if 24 instances had to do completely different things, you can further abstract them in other projects, for example, create 24 objects / buttons that refer to 24 different instances of the class MyOperation1 , MyOperation2 , MyOperation3 ...

IMHO, the key design decision will be: what do you want to centralize? If they all do almost the same thing, you need one class with 24 instances. If they behave differently (you have a completely different logic in each of the case in your switch ), then you can benefit from one or more processing classes that can have a common base class and then create 24 buttons for each link per instance of the processing class.

+1
source

How about an array of strings, where id is the index in the array. So you get the line

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

You just need to somehow initialize it, perhaps read it from a file or database.

+1
source

You can set the button tag property ( android:tag XML attribute) to "car, west, left" and get it using the getTag () method.

+1
source

Source: https://habr.com/ru/post/902045/


All Articles