Using the mouse as a multi-tool creates heavy conditional logic for executing commands, how do I change this?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật