By using the command pattern you are seperating the operation from the invoking object. And just because of that it becomes easier to change the command without chagning the caller/s.
This means that you could use Command pattern when you might have the following situation
You want to parameterize objects to perform an action
You want to specify, execute and queue requests at different times.
Just to quickly start you need a command object, An interface will keep it easy going in this case, thus providing you with the option of extending other classes e.g. Swing MenuItem or Button.
Below the execute Method is the one invoked to do something when this command is called or asked to do its stuff.
Where as the getCommandName is assumed as a unique name how ever I am sure we can always come up with a better implementation for uniqueness.
1 2 3 4 5 6 | public interface Command { public void execute(); public String getCommandName(); } |
And example implementation of the Command should look as follows
A Command Name, and and execute Method to tell what happens when this command is called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class ForwardCmd implements Command { private String COMMAND_NAME = "Back"; public BackCmd() { super(); } public String getCommandName() { return COMMAND_NAME; } public void execute() { System.out.println("Your wish, my command"); } } |
The command manager is the controller in this case. It registers command objects. the “registerCommand” will simply take a command and store it in a list or something alike. This means you could load it out of a jar file, or an xml or path and just pass the object to the “registerCommand” AS a command offcourse.
the “execute” Command will simply execute the Command passed to it.
And the “getCommand” returns a command by looking up a COMMAND_NAME. So if you provide a name to it through you system it should give you an object of type Command and simple pass it to execute. Again this would be a controller logic and not the client one.
1 2 3 4 5 6 7 | public abstract class AbstractCommandManager { public abstract void registerCommand(Command command); public abstract Collection getAllCommands(); public abstract void execute(Command command); public abstract Command getCommand(String name); } |
Tags: command, Computers, Design, Design Patterns, GOF, HOWTO, Java, Patterns, Programming, Software, Software Development
Typically when implementing an interface you would have to implement all the methods that exist in that interface. A very good example is the MouseListener in the java Swing. When you need to implement more then one method where as typically you might be catching only one of them. Saying that you would also find a Mouse Adapter provided as well. Some of us use that often. And that is part of the Adapter pattern. It makes life easier for me sometimes.
Adapter a structural pattern will let you adapt to a different environment. The joining between different environment is called Adapter. Thus basically giving others the interface that they expect or vice versa when your program becomes the client.
For example the following class expects that the implementing class should be implementing all three methods.
1 2 3 4 5 6 7 8 9 | public interface RecordListener { public void eventPrePerformed(RecordEvent recordEvent); public void eventPerformed(RecordEvent recordEvent);} public void eventPostPerformed(RecordEvent recordEvent); } |
So lets say our implementing class is a rude one and only wants to implement one method. What do you do as an API designer. hmmm
Thats where we step in with the Adapter.
1 2 3 4 5 6 7 8 9 10 11 12 13 | public abstract class RecordAdapter implements RecordListener { public void eventPrePerformed(RecordEvent recordEvent) {} public void eventPerformed(RecordEvent recordEvent) {} public void eventPostPerformed(RecordEvent recordEvent) {} } public MyAdapterImpl extends RecordAdapter{ public void eventPerformed(RecordEvent recordEvent){} } |
Now the only thing left to do is use the adapter. And override any method that you might need .
1 2 3 4 5 6 7 8 9 | public MyClientClass { public MyClientClass(){ this.addRecordListener(new MyAdapterImpl()); } } |
Tags: adapter, adapter pattern, Computers, Design, Design Patterns, GOF, HOWTO, Java, Patterns, Programming, Software, Software Development
Sometimes you require to do things silently, without any questions asked and “Just Do It” attitude is required.
I often find my self with this problem.
If you want to delete or create a Websphere profile from your command line try the following. (I have tried on RSA only)
# deleteing a profile
wasprofile -delete -profileName MyProfile
You should get the following message on deletion
INSTCONFSUCCESS: Success: The profile no longer exists.
Creating a websphere profile
wasprofile -create -profileName MyProfile -profilePath \
[PROFILE PATH] -templatePath \
[RSA HOME]runtimes\base_v6\profileTemplates\default \
-nodeName [NODE NAME] -cellName [CELL NAME] -hostName [HOSTNAME].
Tags: Administrator, HOWTO, IBM, Java, jdbc, jython, Programming, scripting, sysadmin, wasprofile, websphere, wsadmin