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...


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.

  • Not sure which language you're using, but might you try using some type of Dictionary/alternate lookup object? You could map 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.
    – user26452
    Commented Feb 27, 2013 at 1:52
  • I'm more concerned with the overall design, so language isn't a big deal. But I'm not exactly sure I understand what you're saying. Commented Feb 27, 2013 at 2:47
  • I'll add an answer with some psuedocode.
    – user26452
    Commented Feb 27, 2013 at 3:07
  • What makes you think that you aren't going to have that logic encoded somehow? All you might do is invert the code so that your generic code just finds the component-specific code to dispatch to. Commented Feb 27, 2013 at 6:16
  • I mean, obviously I won't really get "rid" of the logic. I'm just looking for different solutions to encode it in different ways/places. I've never built anything with a large "command tree" so I'm just kinda putting feelers out there. Commented Feb 27, 2013 at 6:25

3 Answers 3


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.

  • I think an understanding of the Command pattern might help you understand my question better. So would a better example on my part :). Although, I see what you're getting at in a general, a hashmap could only get rid of one layer of conditional logic, there are still a lot of paths to get which Command to execute based on the mouse's target. Commented Feb 27, 2013 at 6:21
  • In a more simple app, where actions are only based on 1 variable, this is definitely the way to go. But I'll end up with (at least) 3 variables (scope, mouse-button, object-state) to determine the click action. Which is leading me more and more to the Factory pattern; one isolated place to create the Commands because their creation logic is complicated. Commented Feb 27, 2013 at 6:21
  • I confess, even though I've read up on the Factory pattern before, I haven't used it and don't have a thorough remembrance of it. (Sigh. I need to remedy that. Today.) However, my gut instinct would be to create or use a data structure that can provide what you're looking for - whether that's some type of multi-node tree, or something else. The way I see it, you're going to have to write that logic somewhere, no matter what, whether it's defined in the program or in an external file. I would personally attempt to bind the logic to a data structure of some sort, instead of inside the code.
    – user26452
    Commented Feb 27, 2013 at 14:35

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.

  • 1
    Yeah, I'm not using a specific framework, and I'm probably a more unusual edge case (interactive music notation on HTML5 Canvas) where most frameworks don't actually give me that useful of a boilerplate. All my views test for click detection, so I know the targets of the specific click at the beginning of the method determineClickCommand(clickedFocus). I figured it would be better to have one isolated place where all Commands are being created based on the mouse's target(s) and their individual states, as opposed to creation being scattered to various views. Commented Feb 27, 2013 at 5:29
  • Can you give an example of a framework which actually does that? I've used about a dozen desktop ones, and none of them work like that to distinguish left/right/middle- they all give a mouse click event on the target, but the buttons pressed are indicated by a bitfield. Commented Feb 27, 2013 at 16:07
  • @PeteKirkham That's exactly what I mean. I wrote the meaning of the event in English to get the idea across. Where the information about the event is stored (which button, mouse location, time, click count, etc.) is an implementation detail. The OP didn't indicate what platform he's working on; it turns out that he's working in HTML I expect there are different options anyway.
    – Caleb
    Commented Feb 27, 2013 at 16:32

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.