Results 1 to 11 of 11
Thread: Command design pattern
- 01-23-2010, 09:48 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
Command design pattern
Hey guys,
I've red about the command design pattern and i want to implement it in java. I think in the command pattern there has to be an interface with a paste( ) mehtod and an object who implements this interface, right? But now i'm stuck: Do you have to make for every button your own subclass of JButton with an actionperformed method who calls the past method of a class who implements the paste method, or can you make an object who implements the paste interface and who makes the actionlistener of the button?
thanks, Hannes
- 01-23-2010, 11:15 AM #2
i think wiki has an exciting example of how to apply the command pattern. take a look
"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 01-23-2010, 05:23 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
Here is very very good example:
Command Pattern-Behavioral patterns
javax.swing.Action class is your command object,
First notice class hierarchy, how interfaces from root one extend each other
public interface Action
extends ActionListener
and
public interface ActionListener
extends EventListener
Of course most important method to implement is well known :
actionPerformed(ActionEvent e)
from the listener interface for receiving action events.
Your buttons use the addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.
does this clear up things for you?
regards
- 01-23-2010, 06:45 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
So you have to make your own action:
ORJava Code:class Command extends AbstractAction{ }
Something like this?Java Code:class Command implements Action{ }
- 01-23-2010, 07:37 PM #5
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
You can use both solutions as I see it,
because
public abstract class AbstractAction
extends Object
implements Action, Cloneable, Serializable
Whatever you choose is OK as long as you
create your own Concrete Command classes
and pass them to the constructors of JButton classes.
Every Concrete Command classes must implement
actionPerformed()
in order to make this work.
- 01-23-2010, 07:52 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
Then, does it make sense if a make a couple of standard buttons: okbutton, cancelbutton, pastebutton... , and each of this buttons own the interface they need, for example the pastebutton holds an interface with an abstract method paste( ), and in the program there is made a concretepastecommand who implements this interface. This concrete command is passed in the constructor of the button. When the actionperformed is invoded, the only thing it does is calling the paste( ) of the concrete command.
Is this good, or is there a better solution?
- 01-23-2010, 08:44 PM #7
before one can judge this, try to explain what you want to achieve.
when i hear copy and paste i think you maybe should google
"swing clipboard" or something..."There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 01-24-2010, 03:03 PM #8
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
It's obvious that you understood story behind command pattern
and you know how to implement it, but do you really need Command
for your paste action and one button - that depends on your needs.
Don't enforce using pattern if there is not need for it.
Define all Actions.
Define all Components.
Make a relation between them. Find what is common.
If you find specific structure or behaviour that can change over time,
and you want to decide about it in runtime not compile time, you have pattern candidate but don't run into it, take your time.
regards
- 01-24-2010, 03:55 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
There is a whole menubar and a toolbar with several menus and menuitems, the paste button was an example.
Oh, one more question: how do you implement this command pattern with wizards? I've looked around, but i didn't find anything helpful.Last edited by hannes; 01-24-2010 at 04:45 PM.
- 01-27-2010, 10:54 PM #10
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
I'm afraid you will not find that kind of wizard
because main purpose of wizard does not have anything to do
with principles of your code design.
But i recommend you to take a look how Observer patterns is used in
application of your type it might be interesting.
- 01-28-2010, 06:41 AM #11
Member
- Join Date
- Nov 2009
- Posts
- 54
- Rep Power
- 0
I now work in a different way: There is a view, and each view has its own exector. The actionListeners of the buttons of the view are inside the executor. The executor owns the main-object: Application. Every view has its executor.
The executor finds its way through the objects via the get( ) methods of Application, who contains all the executors and objects who haven't got a view.
I think this is a good way of working, for me, it works fine.
What do you think of this?
HannesLast edited by hannes; 01-28-2010 at 07:19 PM.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks