Implement command pattern in any way or anything similar or anything at all

I have a controller class that listens to button presses from my button panel, and it also listens to mouse actions from another drawing panel.

When I press a button, it indicates some kind of drawing is going to happen in the drawing area. Only one action could be "active" at the same time. (To exemplify, put out square, put out circle, or whatever).

First I thought of using something similar to the command pattern. But when I press the button (here I wanted to create the command since then I know which one) I dont know yet which coordinates that are of interest. That would mean I would have to pass that data into the command磗 execute() method, or create the command first now (right before it should execute)

Any suggestions of what could be a nice approach here are very welcome.

I know I am in that phase that I want to "patternize" a bit to much, but hey, at least you learn something and eventually you learn when not/how to use it more properly.

[991 byte] By [sandsatera] at [2007-10-3 2:48:35]
# 1
A command pattern sounds like a good solution to this problem.I would suggest your option of having an execute method that took in coordinates.
zadoka at 2007-7-14 20:37:22 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

I tried that and my code is much cleaner now. The only drawback was that some commands were to be executed when clicking in the area, and some when moving the mouse after clicking. That gave me

if(CommandInterface instanceof ComplexCommand){

..

}

in a place or two which feels like violating the pattern but I couldn磘 think of a better way and it still is better than before.

sandsatera at 2007-7-14 20:37:22 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> I tried that and my code is much cleaner now. The

> only drawback was that some commands were to be

> executed when clicking in the area, and some when

> moving the mouse after clicking. That gave me

> if(CommandInterface instanceof

> ComplexCommand){

> ..

> }

>

> in a place or two which feels like violating the

> pattern but I couldn磘 think of a better way and it

> still is better than before.

Glad that it worked good.

Sometimes instanceof is needed but it is typically a bad smell for me. Maybe you could have a method that would return when the command needed to be excuted or could pass something else to the command that it could use determine when it needed to be executed? (Just some ideas off the top of my head).

zadoka at 2007-7-14 20:37:22 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Maybe you could have a method that would return when the command needed to be excuted.

I dont get you fully there.

or could pass something else to the command that it could use determine when it needed to be executed?

I will however try to also send a bool which indicates whether it comes from a click or not. If that succeeds, I have managed to get all the commands to know by themselves when to do or not to do their stuff.

The only things that feels bad about that, is that only one command (for the moment at least) makes use of it. But I get rid of the bad smell ;-)

Add: Turned out that after all changes, most of the commands needed this variable too. Now everything feels good again ;)

Message was edited by:

sandsater

Message was edited by:

sandsater

sandsatera at 2007-7-14 20:37:22 > top of Java-index,Other Topics,Patterns & OO Design...