The feature is in many different types of editing programs where a mouse click may have completely different commands to execute (using the Command Pattern)
Currently I have an overarching determineClickCommand(clickFocus)
function.
The body essentially looked like this:
var command = null;
var args = null;
if (clickFocus == x) {
if (x.state == state) {
args = // Code to detemine args
command = new CommandA(args)
} else {
command = new CommandB()
}
} else if (clickFocus == y) {
args = blahablah;
command = new CommandC(args)
} else if (clickFocus == z) {
command = new CommandD()
} ...etc..etc...etc...
command.execute()
Which was fine when I was prototyping, but I’d like a more elegant solution. Particularly since at some point, I’m going to want to take advantage of right-click/middle-click, which could add a third nested if statement
My first idea is to create a AbstractClickScope
class with a determineClickCommand()
method to be overridden in each concrete scope subclass. This breaks the problem into more maintainable classes, but the extensive conditional logic is the exact same. And if I were to decide to have many ‘scopes’ then a new class each time would be very annoying.
Or maybe have some sort of command factory class as opposed to a method? This isolates Command creation to a very specific part of the code.
Any other ideas? Or known implementations? Just throwing ideas out there and looking for more info as I think through this.
5
You’re looking for a way to eliminate some code bloat and simplify your interface, which is great. My suggestion is, instead of using if/else
statements or switch/case
blocks, use something more maintainable like a Dictionary/hashmap.
Here’s the general idea of using a lookup hash. (This is pseudocode, so please excuse any syntax discrepancies and omitted code blocks.) Let’s say you have the following classes:
class A {
method1 () {}
method2 () {}
}
class B {
method3 () {}
method4 () {}
}
Instead of saying this:
if (clickFocus == x) {
if (x.state == state) {
// execute A.method1
} else {
// execute A.method2
}
} else if (clickFocus == y) {
// execute B.method3
} else if (clickFocus == z) {
// execute B.method4
} ...etc..etc...etc...
…you could store these calls in a hashmap with key/value pairs, like so:
class MouseHandler {
public myHashMap = new Hashmap;
myHashMap.x = A.method1; // Reference (or pointer) to function
myHashMap.y = A.method2; // or possibly function body itself
myHashMap.z = B.method3;
}
Then, you can simply pass your clickFocus to the map as a parameter, which will return the function you want. So you’d have something like:
function getCommandForMouseEvent(clickFocus) { // return a function reference
return MouseHandler.myHashMap[clickFocus];
}
Note that this is really simplified, but doing something like this should cut down on your code a bit. (This may actually match one of the design patterns you mentioned. I really need to read up on those.) The other nice thing about this is that you can add keys to your hash during runtime, so you can register new functionality.
I hope that made some sense, and I hope it serves as a good starting point.
3
This is one of the jobs that graphical user interface frameworks do for you. A typical GUI framework will employ a hierarchy of views and a way to find the particular view for a given coordinate. When the mouse does something (moves, button down, button up, etc.) the framework finds the relevant view and sends it an appropriate message, like “the left mouse button went down at local coordinates (127, 225)”. It’s then up to the view to decide what to do with that information, and each view is free to take whatever action is necessary. For example, a button might ignore the coordinates and just redraw itself in a selected state, while a text view might move the insertion point.
If you’re working with a GUI framework, you shouldn’t have to worry too much about dealing directly with the mouse. Find out how your framework does it and employ that mechanism. If you’re trying to write your own framework, it’d probably be a good idea to survey two or three existing frameworks and find out how they work — you’ll find that there’s a lot of commonality, and you’ll probably want your framework to do roughly the same thing.
3
You could use the State design pattern for this. It looks like you have a different situation for each value of clickFocus
, so you could put the code to handle the click onto those objects. This removes that huge chain of if-else
cases in the method.