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.
clickfocus
keys to function references or something like that, which would reduce a lot of code. A setup like this would let you register new handlers easily with key/value pairs.