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

  • SEARCH
  • Home
  • 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 187029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:45:04+00:00 2026-05-11T15:45:04+00:00

I have a class that should read a message, extract its type from the

  • 0

I have a class that should read a message, extract its type from the envelope and then call a method that have to process the message; at each message type is associated an enum member.

There are 30+ message types.

One way to dispatch the message is to just use a switch, but it’s really ugly and error-prone (have I already tract that case? have I tract that case two times?).

One another way is to define an interface with a single method process(data) and create one class for each message type that implements that interface, register those classes with a map and, in the code that should process the message, just call map.get(messageType).process(data); but it’s really annoying to create 30+ classs.

One another way is to use reflection: define one function for each messagetype, with a precise signature and pattern name, like processMessagetype; then create a map from messagetype to Method (filled with a simple search upon getDeckaredMethods()) and then do something like: map.get(messageType).invoke(this,data).

What method you should use? What’s the overhead of using java’s reflection?

  • 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. 2026-05-11T15:45:05+00:00Added an answer on May 11, 2026 at 3:45 pm

    Reflection has a big overhead if you’re looking for good performance. If your routine (message dispatch) is going to be used over and over the polymorphism alternative is better. Also, IMO it’s better if looking in a OO perspective, since as stated by Brian on his answer, it’s easier and cheaper for maintenance.

    Your clas handling 30+ messages can easily grow to 100+ messages, then you will have a class with 100+ methods, add the private methods too and code reuse will be hard and your class has just became a mess.

    The overhead of reflection over polymorphism and switch is something like this:

    time spent on a switch case call: 1 time spent on a polymorphism call: 1.1 time spent on a reflection call: 1.9-2.0 

    I benchmarked these guys a while ago, and those were my results, I don’t have the code now because these benchmarks were done on my previous company and I don’t have access to that code anymore.

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

Sidebar

Ask A Question

Stats

  • Questions 93k
  • Answers 93k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suggest running your code under CLR Profiler to locate… May 11, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer You would need an instance of the mypage class or… May 11, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer I usually solve the ctrl-a problem by setting my escape… May 11, 2026 at 6:42 pm

Related Questions

I have a Status class in C#, used like this: Status MyFunction() { if(...)
I have an application written in C#.Net (Framework 2.0 if it matters). It calls
When reading data in chunks of say, 1024, how do I continue to read
Im not sure if i have explaned this the best i can but, here
I'm somewhat new to Python, Django, and I'd like some advice on how to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.