Board index » jbuilder » Using map to call methods in a class (tutorial/source example)

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.
 
 

Re:Using map to call methods in a class (tutorial/source example)

On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:
Quote
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.
You probably want to create an interface (or an abstract base class) and
then create an implementation (concrete subclass) for each command. Then
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
 

Re:Using map to call methods in a class (tutorial/source example)

John McGrath [TeamB] wrote:
Quote
On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:


>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.


You probably want to create an interface (or an abstract base class) and
then create an implementation (concrete subclass) for each command. Then
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.

So something like this. (have not checked into map methods so guessing.)
Command currentCommand = (Command) map.get( "login" );
currentCommand.execute();
thats very close to what I did in C++.
Am I close..
 

{smallsort}

Re:Using map to call methods in a class (tutorial/source example)

John McGrath [TeamB] wrote:
Quote
On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:


>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.


You probably want to create an interface (or an abstract base class) and
then create an implementation (concrete subclass) for each command. Then
add the implementations to the Map. For example:


public interface Command {
void execute();
}
can I add methods and attributes to the class above. Or do I have to
created a different class to do this such as:
public interface Command {
string className;
string Text;
void execute();
void doSomething();
}
Quote
:
:
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.

 

Re:Using map to call methods in a class (tutorial/source example)

On 1/19/2005 at 3:06:05 PM, Joe Vasher wrote:
Quote
can I add methods and attributes to the class above. Or do I have to
created a different class to do this such as:

public interface Command {
string className;
string Text;
void execute();
void doSomething();
}
Of course. However, if you use an interface, you will need to implement
the methods in *each* subclass. If some of the behaviors would be the
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
 

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:
Quote
you will need to implement the methods in each subclass.
Terminology correction: this should read "in each implementing class".
You do not *subclass* an interface, you *implement* it.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Using map to call methods in a class (tutorial/source example)

Joe Vasher wrote:
Quote
John McGrath [TeamB] wrote:

>On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:
>
>
>>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.
>
>
>
>You probably want to create an interface (or an abstract base class) and
>then create an implementation (concrete subclass) for each command. Then
>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.
>
So something like this. (have not checked into map methods so guessing.)

Command currentCommand = (Command) map.get( "login" );

currentCommand.execute();

thats very close to what I did in C++.

Am I close..
So would this be a better way to handle commands if i'm only going to
handle 1 command at a time and don't want to chew up memory with
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;
}
 

Re:Using map to call methods in a class (tutorial/source example)

Joe Vasher wrote:
Quote
Joe Vasher wrote:

>John McGrath [TeamB] wrote:
>
>>On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:
>>
>>
>>>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.
>>
>>
>>
>>
>>You probably want to create an interface (or an abstract base class) and
>>then create an implementation (concrete subclass) for each command.
>>Then
>>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.
>>
>So something like this. (have not checked into map methods so guessing.)
>
>Command currentCommand = (Command) map.get( "login" );
>
>currentCommand.execute();
>
>thats very close to what I did in C++.
>
>Am I close..


So would this be a better way to handle commands if i'm only going to
handle 1 command at a time and don't want to chew up memory with
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;

}

Left a little code out of the command handler such that I would call
from the loop for reading from the socket I would do something like this
string command = "login";
CommandHandler currentCommand = new CommandHandler();
currentCommand = (Command) map.get( command );
HMMM... Thought I had it, but that don't look right.
 

Re:Using map to call methods in a class (tutorial/source example)

Joe Vasher wrote:
Quote
Joe Vasher wrote:

>Joe Vasher wrote:
>
>>John McGrath [TeamB] wrote:
>>
>>>On 1/18/2005 at 4:18:40 PM, Joe Vasher wrote:
>>>
>>>
>>>>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.
>>>
>>>
>>>
>>>
>>>
>>>You probably want to create an interface (or an abstract base class)
>>>and
>>>then create an implementation (concrete subclass) for each command.
>>>Then
>>>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.
>>>
>>So something like this. (have not checked into map methods so guessing.)
>>
>>Command currentCommand = (Command) map.get( "login" );
>>
>>currentCommand.execute();
>>
>>thats very close to what I did in C++.
>>
>>Am I close..
>
>
>
>So would this be a better way to handle commands if i'm only going to
>handle 1 command at a time and don't want to chew up memory with
>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;
>}
>
Left a little code out of the command handler such that I would call
from the loop for reading from the socket I would do something like this

string command = "login";

CommandHandler currentCommand = new CommandHandler();

currentCommand = (Command) map.get( command );

HMMM... Thought I had it, but that don't look right.
Is the above correct. Here is my problem i have read several tutorials
on interface and still can't grasp what Is going on. I just read this 1
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.
 

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:
Quote
So would this be a better way to handle commands if i'm only going to
handle 1 command at a time and don't want to chew up memory with
abstracting a class. (Ignore syntex I keep confusing C++ with java)
I really would not worry too much about the cost of creating new classes
in Java. They are really not that expensive, so you do not save that
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.
Quote
class commandHandler implements Command {
:
}
You define the commandHandler class here, but you never use it, so I am
not quite sure what you intended. If you change the 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:
Quote
Left a little code out of the command handler such that I would call
from the loop for reading from the socket I would do something like this

string command = "login";

CommandHandler currentCommand = new CommandHandler();

currentCommand = (Command) map.get( command );

HMMM... Thought I had it, but that don't look right.
Yes, this is pretty much right. However, you would want to use the
Command interface here, rather than the implementing class.
On 1/21/2005 at 10:14:46 AM, Joe Vasher wrote:
Quote
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.
It doesn't. The Company object asks the Employee object it is given to
doWork(). It is the Employee object itself that knows how to do the work.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

Re:Using map to call methods in a class (tutorial/source example)

On 2/14/2005 at 11:48:57 AM, Joe Vasher wrote:
Quote
I added the execute() method just like your example. JBuilder X is
giving this error. (I recompiled everything).
That code looks good -- try a full rebuild. I have seen JBuilder's
dependency checker get confused and not recompile code that needs to be
recompiled.
--
Regards,
John McGrath [TeamB]
---------------------------------------------------
Before sending me e-mail, please read:
www.JPMcGrath.net/newsgroups/e-mail.html
 

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:
Quote
On 1/19/2005 at 3:06:05 PM, Joe Vasher wrote:
Of course. However, if you use an interface, you will need to implement
the methods in *each* subclass. If some of the behaviors would be the
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.

 

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() );
}
}
 

Re:Using map to call methods in a class (tutorial/source example)

On 2/14/2005 at 8:19:41 PM, Joe Vasher wrote:
Quote
It seems I didn't add it correctly to JBUILDER I went through and
manually compile those classes it works fine. But is it not possible to
get jbuilder to build these. If so what am I not doing to get jbuilder
to hand it.

I keep Test.java in my documents and settings directory I have on my
D:\java\com.specone.util both CommandLoader and Command. Which was added
to my project and appears in the project pane the source code for both
those. I also added D:\Java to library Menu Item (tools/configure
libraries) To add the source code to my project, I right clicked in the
project pane and selected add files/packages/classes then selected from
the directory tree the two above mentioned source files. Am I missing
something.
You have a directory named "com.specone.util"? Source files are supposed
to be placed in a directory structure that matches the package structure,
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
 

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();
}