Results 1 to 11 of 11
  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Question 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

  2. #2
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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 ?"

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  4. #4
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    So you have to make your own action:

    Java Code:
    class Command extends AbstractAction{
    }
    OR

    Java Code:
    class Command implements Action{
    }
    Something like this?

  5. #5
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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.

  6. #6
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    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?

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    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 ?"

  8. #8
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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

  9. #9
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    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.

  10. #10
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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.

  11. #11
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default

    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?
    Hannes
    Last edited by hannes; 01-28-2010 at 07:19 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •