Board index » jbuilder » Using map to call methods in a class (tutorial/source example)
|
Joe Vasher
JBuilder Developer |
|
Joe Vasher
JBuilder Developer |
Using map to call methods in a class (tutorial/source example)2005-01-19 05:18:40 AM jbuilder0 I'm trying to write a program that will allow me to create map to handle commands as they come in from the server. I would like to have the command looked up in a map list and call the attached method. Is this possible and if so, is there a tutorial/source example which would help me understand this. Thanks. |
| John McGrath [TeamB]
Java Developer |
2005-01-19 07:59:46 AM
Re:Using map to call methods in a class (tutorial/source example)
On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:
QuoteI'm trying to write a program that will allow me to create map to handle add the implementations to the Map. For example: public interface Command { void execute(); } : : map.put( "login", new Command() { public void execute() { // implement login } } ); map.put( "logout", new Command() { public void execute() { // implement logout } } ); Then you can look up commands in the map, retrieving a Command object. If found, you can call the execute() method on the retrieved object. -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| Joe Vasher
Java Developer |
2005-01-20 12:57:42 AM
Re:Using map to call methods in a class (tutorial/source example)
John McGrath [TeamB] wrote:
QuoteOn 1/18/2005 at 4:18:40 PM, Joe Vasher wrote: currentCommand.execute(); thats very close to what I did in C++. Am I close.. {smallsort} |
| Joe Vasher
Java Developer |
2005-01-20 04:06:05 AM
Re:Using map to call methods in a class (tutorial/source example)
John McGrath [TeamB] wrote:
QuoteOn 1/18/2005 at 4:18:40 PM, Joe Vasher wrote: public interface Command { string className; string Text; void execute(); void doSomething(); } Quote: |
| John McGrath [TeamB]
Java Developer |
2005-01-20 05:02:02 AM
Re:Using map to call methods in a class (tutorial/source example)
On 1/19/2005 at 3:06:05 PM, Joe Vasher wrote:
Quotecan I add methods and attributes to the class above. Or do I have to same for all Command instances, you probably want to use an abstract base class. For example: public abstract class Command { private String name; public Command( String name ) { this.name = name; } public String getName() { return name; } public abstract void execute(); } Command loginCommand = new Command( "login" ) { public abstract void execute() { // Execute the login. } }; Then you might want to populate the command map with something like this: Command[] COMMANDS = new Command[] { loginCommand, logoutCommand, ... } for ( Command command: COMMANDS ) { commandMap.put( command.getName(), command ); } Note that the above uses the Java 5 enhanced for loop feature. For earlier JDKs, you will need to adjust the code. -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| John McGrath [TeamB]
Java Developer |
2005-01-20 05:03:58 AM
Re:Using map to call methods in a class (tutorial/source example)
On 1/19/2005 at 4:02:02 PM, John McGrath [TeamB] wrote:
Quoteyou will need to implement the methods in each subclass. -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| Joe Vasher
Java Developer |
2005-01-20 10:38:39 PM
Re:Using map to call methods in a class (tutorial/source example)
Joe Vasher wrote:
QuoteJohn McGrath [TeamB] wrote: abstracting a class. (Ignore syntex I keep confusing C++ with java) public interface Command { void execute(); } classs MyClass { MyClass() { map.put( "login", new Command() { public void execute() { // implement login } } ); map.put( "logout", new Command() { public void execute() { // implement logout } } ); } main(){ ... } } class commandHandler implements Command { String Name; String commandType; String Text; } |
| Joe Vasher
Java Developer |
2005-01-21 01:38:13 AM
Re:Using map to call methods in a class (tutorial/source example)
Joe Vasher wrote:
QuoteJoe Vasher wrote: string command = "login"; CommandHandler currentCommand = new CommandHandler(); currentCommand = (Command) map.get( command ); HMMM... Thought I had it, but that don't look right. |
| Joe Vasher
Java Developer |
2005-01-21 11:14:46 PM
Re:Using map to call methods in a class (tutorial/source example)
Joe Vasher wrote:
QuoteJoe Vasher wrote: trying to answer the above question. if it is correct? webchalkboard.tripod.com/java/lesson1/tute9.shtml interface Employee { public void doWork(); public void getPay(); } class Person implements Employee { public void doWork() { System.out.println("Working"); } public void getPay() { System.out.println("Spending"); } } class Company { Employee emp; public void hire(Employee e) { emp=e; } public void makeMoney() { emp.doWork(); emp.getPay(); } } In the above how does the class company KNOW to use the Class Person to work out the definitions for emp.doWork(); and emp.getPay is it because the compiler uses last know defenition or how did in this code above he tell the compiler to use that definition. |
| John McGrath [TeamB]
Java Developer |
2005-01-23 06:53:15 AM
Re:Using map to call methods in a class (tutorial/source example)
Hi Joe,
A few comments before I answer your questions: 1) You posted your first message of Jan 20th as a reply to your own message, which makes it somewhat less likely that it will be seen in a timely manner. Had you replied to my message, the message and the newsgroup that contains it would have shown up in bright pink in my newsreader, and I would likely have seen it two days ago. I usually read direct replies to my messages before any others. 2) The point of quoting from a message is to show the context of the reply. Quoting the entire message you are replying to provides no new information, since it is already clear that you are replying to that message. All of the extra quoted text just takes up bandwidth and makes it difficult to see the original text. And my index finger is getting tired from scrolling the mouse wheel all the way past the quoted text! :o) On 1/20/2005 at 9:38:39 AM, Joe Vasher wrote: QuoteSo would this be a better way to handle commands if i'm only going to much. Rather than trying to save a few bytes, I think it is much better to make the design maintainable, flexible and extensible. After all, memory is pretty cheap these days. As for handling only a single command at a time, I do not understand how subclassing has much to do with that. And also note that using an interface is not any more efficient than using an abstract base class. In fact, if there is common functionality among the classes, using a base class allows you to define that functionality in one place, rather than duplicating it for each implementation class. Also, are you aware that when you write code like: new Command() { ... }, you are defining a new class? This is called an anonymous class, because the class that you create has no specified name. Quoteclass commandHandler implements Command { implementations to use this as the base class, then it makes a lot of sense. Defining an interface, and then an abstract class that implements some of the functionality required by the interface is a very good design approach. In fact, I would say it is much better than using just an interface or just an abstract base class. If you took my earlier comments to suggest that it was an either-or situation, I am sorry - I did not mean that. When you do that, the interface defines what is needed by code that uses a Command, and it imposes no restrictions on how they are implemented. The abstract base class provides some of the functionality that is required by the interface, so implementations do not have to provide it in each class that implements the interface. You can find many examples of that in the Java APIs, such as the TableModel interface and the AbstractTableModel class. On 1/20/2005 at 12:38:13 PM, Joe Vasher wrote: QuoteLeft a little code out of the command handler such that I would call On 1/21/2005 at 10:14:46 AM, Joe Vasher wrote: QuoteIn the above how does the class company KNOW to use the Class Person to -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| John McGrath [TeamB]
Java Developer |
2005-02-14 06:47:51 PM
Re:Using map to call methods in a class (tutorial/source example)
On 2/14/2005 at 11:48:57 AM, Joe Vasher wrote:
QuoteI added the execute() method just like your example. JBuilder X is recompiled. -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| Joe Vasher
Java Developer |
2005-02-14 07:58:35 PM
Re:Using map to call methods in a class (tutorial/source example)
This is where I am at.
I have written two classes Command and CommandLoader Inside CommandLoader I put main strictly for testing purposes. I have ran into a little problem I'm not understanding. Here is the classes //CommandLoader package com.specone.util; import java.util.*; import com.specone.util.*; public class CommandLoader { private static final int LOGIN = 0; private TreeMap commandMap = new TreeMap(); CommandLoader() { commandMap.put( "LOGIN", new Command() ); } static public void main( String[] args ) { System.out.println( "Hello World! " + commandMap.get( "LOGIN" ).name ); } } //Command package com.specone.util; public class Command { public String name = "TESTING"; //abstract public void execute(); } This class will later have the abstract method execute but for now one problem at a time. HERE IS THE ERROR: D:\Java\com\specone\util\CommandLoader.java:23: non-static variable commandMap cannot be referenced from a static context System.out.println( "Hello World! " + commandMap.get( "LOGIN" ).name ); ^ D:\Java\com\specone\util\CommandLoader.java:23: cannot resolve symbol symbol : variable name location: class java.lang.Object System.out.println( "Hello World! " + commandMap.get( "LOGIN" ).name ); ^ 2 errors Tool completed with exit code 1 John McGrath [TeamB] wrote: QuoteOn 1/19/2005 at 3:06:05 PM, Joe Vasher wrote: |
| Joe Vasher
Java Developer |
2005-02-14 09:58:24 PM
Re:Using map to call methods in a class (tutorial/source example)
For the above problem I made some changes, it fixed the static problem
but throws and exception now.. // TEST import java.awt.*; import com.specone.util.*; class Test { static public void main( String[] args ) { CommandLoader cl = new CommandLoader(); Command tCmd = (Command)cl.commandMap.get( "LOGIN" ); System.out.println( "Hello World! " + tCmd.name ); } } //Command package com.specone.util; public class Command { public String name; Command() { name = "TESTING"; } } //CommandLoader package com.specone.util; import java.util.*; import com.specone.util.*; public class CommandLoader { public TreeMap commandMap; public CommandLoader() { commandMap = new TreeMap(); commandMap.put( "LOGIN", new Command() ); } } |
| John McGrath [TeamB]
Java Developer |
2005-02-14 10:16:33 PM
Re:Using map to call methods in a class (tutorial/source example)
On 2/14/2005 at 8:19:41 PM, Joe Vasher wrote:
QuoteIt seems I didn't add it correctly to JBUILDER I went through and with a directory for each subpackage. In other words, source files in the package "com.specone.util" should go in directory "com/specone/util", not "com.specone.util". These files should also be placed under one of the source root directories for the project. The default source path contains the directory "src", so you would place source files for the package "com.specone.util" in the directory "<ProjectRoot>/src/com/specone/util", where <ProjectRoot>is the directory that contains your project file. -- Regards, John McGrath [TeamB] --------------------------------------------------- Before sending me e-mail, please read: www.JPMcGrath.net/newsgroups/e-mail.html |
| Joe Vasher
Java Developer |
2005-02-15 12:48:57 AM
Re:Using map to call methods in a class (tutorial/source example)
I added the execute() method just like your example. JBuilder X is
giving this error. (I recompiled everything). "Test.java": cannot resolve symbol: method execute ()in class com.specone.util.Command at line 13, column 10 "Test.java": cannot resolve symbol: method execute ()in class com.specone.util.Command at line 16, column 10 // TEST import java.awt.*; import com.specone.util.CommandLoader; import com.specone.util.Command; class Test { static public void main( String[] args ) { CommandLoader cl = new CommandLoader(); Command tCmd = (Command)cl.commandMap.get( "LOGIN" ); tCmd.execute(); tCmd = (Command)cl.commandMap.get( "LOGOFF" ); tCmd.execute(); } } //CommandLoader package com.specone.util; import java.util.*; import com.specone.util.*; public class CommandLoader { public TreeMap commandMap; public CommandLoader() { commandMap = new TreeMap(); commandMap.put( "LOGIN", new Command() { public void execute() { System.out.println( "LOGIN" ); } } ); commandMap.put( "LOGOFF", new Command() { public void execute() { System.out.println( "LOGOFF" ); } } ); } } //Command package com.specone.util; public abstract class Command { public abstract void execute(); } |
