Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Any way to map method calls?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Any way to map method calls?

    I was wondering whether it was possible to have method calls as the value in a HashMap (or LinkedHashMap). For example, I'm designing a virtual PDA program for a uni assignment. One of my classes is a 'Menu' class which acts as sort of a template for the PDA class's menus. What I'm aiming for is a HashMap in the Menu class, where the key is the menu option (type String) and the value is the method called by selecting that option. The PDA class will then create a bunch of Menus, and fill each Menu's map with the Menu options and the methods called by those options.

    Basically I think I want a field like this in the Menu class:

    private HashMap<String, [Method Call]> menuOptions


    And then have a method in the PDA class that does something like this:

    menuOptions.put("Option A", doSomething());

    where 'Option A' is the name of the option, and 'doSomething()' is a method signature.


    Thanks in advance to anyone who can help me.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Any way to map method calls?

    Well you could rather just have an interface like for instance called MenuOption with a method on it.

    public interface MenuOption {
        public void execute();
    }

    Then you would create your menuoptions to implement this interface and then store the reference to that object in the map.

    public class MyMenuOption implements MenuOption {
     
        public void execute() {
            System.out.println('Execute in MyMenuOption was invoked');
        }
    }

        Map<String, MenuOption> menuOptions = new HashMap<String, MenuOption>();
     
        MyMenuOption myMenuOption = new MyMenuOption();
     
        menuOptions.put("Option A", myMenuOption);

    And then in the menu whenever someone clicks a menu item.

        MenuOption clickedMenuOption = menuOptions.get(keyOfMenuOption);
        clickedMenuOption.execute();

    Something like that might do.

    // Json

Similar Threads

  1. [SOLVED] Method declaration in Java
    By mohsendeveloper in forum Object Oriented Programming
    Replies: 4
    Last Post: June 11th, 2009, 03:18 AM
  2. How to display the contents of my queue?
    By rocafella5007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2009, 11:46 AM
  3. problem with Scanner nextLine() method
    By mia_tech in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 10th, 2009, 04:14 AM
  4. Replies: 2
    Last Post: March 4th, 2009, 06:32 AM
  5. Replies: 1
    Last Post: December 30th, 2008, 07:30 AM