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 355427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:01:00+00:00 2026-05-12T12:01:00+00:00

I have been trying to figure out a way to tag several methods from

  • 0

I have been trying to figure out a way to tag several methods from my base class, so that a client class can call them by tag. The example code is:

public class Base {
         public void method1(){     
        ..change state of base class
    }

    public void method2(){      
        ..change state of base class
    }

    public void method3(){      
        ..change state of base class
    }
}

A client class from a main() method will call each method of Base through a random instruction sequence:

public static void main(String[] args) {
String sequence = "ABCAABBBABACCACC"
Base aBase = new Base();
for (int i = 0; i < sequence.length(); i++){
            char temp = sequence.charAt(i);
            switch(temp){
            case 'A':{aBase.method1(); break;}
            case 'B':{aBase.method2(); break;}
            case 'C':{aBase.method3(); break;}          }
        }

        System.out.println(aBase.getState());

    }

Now I wish to get rid of the switch statement altogether from the Client object. I am aware of the technique to replace switch by polymorphism, but would like to avoid creating a set of new classes. I was hoping to simply store those methods in an appropriate data structure and somehow tag them with a matching character from the sequence.

A map could easily store objects with value/key pairs which could do the job, (as I did here), or the command pattern, but since I don’t want to replace those methods with objects, is there a different way perhaps, to store methods and have a client selectively call them?

Any advice is appreciated

  • 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-12T12:01:00+00:00Added an answer on May 12, 2026 at 12:01 pm

    Something like this?

    public class Base {
    
        private final Map<Character, Method> methods = new HashMap<Character, Method>();
    
        public Base() throws SecurityException, NoSuchMethodException {
            methods.put('A', getClass().getMethod("method1"));
            methods.put('B', getClass().getMethod("method2"));
            methods.put('C', getClass().getMethod("method3"));
        }
    
        public Method getMethod(char c) {
            return methods.get(c);
        }
    
        public void method1() {}
    
        public void method2() {}
    
        public void method3() {}
    
    }
    

    and then

        public static void main(String[] args) throws Exception {
            String sequence = "ABCAABBBABACCACC";
            Base aBase = new Base();
    
            for (int i = 0; i < sequence.length(); i++) {
                char temp = sequence.charAt(i);
                aBase.getMethod(temp).invoke(aBase);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure this out for way to long tonight. I
I have been trying to figure out how to open a file from the
I have been trying to figure out a way to refresh internest explorer through
I have been trying to figure out a way to convert bitmap files into
I have been trying out to figure out a way to find intersection of
I have been trying to figure out a way to manage our domains at
Ok so I have been trying to figure out a way to make plain
I have been trying to figure out how to get files that are at
I have been trying to figure out the best way to start working on
I have been trying to figure out a way to make wget only create

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.