jQuery in Action, 2nd edition
The moose likes OO, Patterns, UML and Refactoring and the fly likes A question about the Command Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Reply Bookmark "A question about the Command Pattern" Watch "A question about the Command Pattern" New topic
Author

A question about the Command Pattern

Patrick O'Hara
Greenhorn

Joined: May 08, 2001
Posts: 9
I have run into a problem several times and I am wondering if a pattern exists to deal with this problem. In general the problem has to do with registering a series of classes of a given type within a framework.
Let me give a concrete example. I am responsible for maintaining a scheduler that my company has written. Not surprisingly, the scheduler works with classes of type Job that fit into JobCategories. We currently have a hardcoded list of JobCategories and Jobs within each Category. Ironically, I have run into the same problem again as I work on a terminal interface for the scheduler. The terminal is using the Command pattern. I have several Commands that I wish to use. I currently have the list of commands in a property file. Though this is better than the hard coded list of Job types within the scheduler, it still seems crude and cumbersome to me.
Does anyone know of a better pattern for dealing with registering object handlers?

------------------
Pat O
pdohara@techneex.com


Pat O<br />pdohara@practicalprogrammer.org
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6035
I'm not sure I quite undertsand what the issue is, but there's something that may help you think about it.
Patterns define object relationships, who has references to whom, and what each class is responsible for.
Patterns are somewhat orthogonal to data storage, though not completely, it and varies by pattern.

For example, let's suppose I have a dozen commands. Ican either create a dozen classes (all extending from an abstract Command class), or I can create one class, and simply have it's fields diffrent for different commands. The data itself can be contained in the program, entered by the user, or stored in a property file. The choice of whether to have 12 classes or 1, depends on how similar or different those commands are.
Maybe this helped. Good luck.
--Mark
Patrick O'Hara
Greenhorn

Joined: May 08, 2001
Posts: 9
Originally posted by Mark Herschberg:
I'm not sure I quite undertsand what the issue is, but there's something that may help you think about it.

The issue is that I have a command factory and I want to create a help command. I thought the help command should be able to ask the command factory what commands are available. That's where I ran into a wall. I can hard code a reference to all the commands into the command factory . I can keep a list of the commands in some external data store (i.e. a properties file) , or I could have the command factory open up the directory for the commands package . I am not crazy about the property file as it could easily get out of date, but I have yet to come up with a better solution.

------------------
Pat O
pdohara@techneex.com
[This message has been edited by Patrick O'Hara (edited July 06, 2001).]
 
 
subject: A question about the Command Pattern
 
Threads others viewed
Cmd alternative
NoSuchClassFound exception
Telnet server in Java
Job Scheduler
trigger programs to run java batch job
developer file tools