Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:46:51+00:00 2026-05-23T17:46:51+00:00

I am currently coding an Android app, which will be used to count Traffic

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:46:51+00:00Added an answer on May 23, 2026 at 5:46 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently coding a button which will delete a record from a database if
I am currently coding a simple Data Access Layer, and I was wondering which
I am currently coding a website for a band and i have a music
I'm currently coding a php website. however one of the requirement is to have
I'm working with Android currently, coding in Eclipse. I'm trying to set an image
I have an Android application in which I'm trying to parse a small sized
I'm currently coding my design portfolio and have encountered a problem with the layout.
Currently my workflow with Emacs when I am coding in C or C++ involves
We are currently going through the long process of writing some coding standards for
I've been working through the Stanford iPhone Coding course and currently hooking into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.