English 中文(简体)
Photoshop style GUI design
原标题:

In a tool such as Photoshop, there is a selection of tools you can click on (e.g. pen, brush etc). Once you click on a tool, you can use that tool to paint, erase etc depending on what tool is selected.

I m wondering how that would be best implemented in an OO design. I could only think of having a GUIManager that always knows which tool is selected, then when the Canvas (drawing area) detects a mouse click or mouse drag it asks GUIManager which tool is selected and applies that tool s behavior.

Does anyone describe a possible solution in class level detail (or in any detail if you don t have time).

最佳回答

Take a look at the State Pattern which has exactly your example.

However, it s not actually a GUI question but more to an object-oriented approach to how to model this problem. The GUI doesn t really play into this.

问题回答

I m not sure OO principles really play into this except in the sense that all graphic elements are probably derived from common base classes in your GUI of choice.

The amount of complexity in an application that needs a hierarchy of screen menus à la photoshop would presumably dwarf the difference between composition vs inheritance vs functional-style vs whatever-you-want.

Although, should you be motivated to write the program twice using different styles, it might be interesting to see the results. The ranking of various programming styles is an open question without real answers at this point, although we think that OO-styles are important in a way we haven t yet completely quantified.

One idea for this would be to have a tool class. Just sketching this in C++-like pseudocode:

class Tool
{
public:
    // Keep a pointer to the "document", i.e. some representation of the
    // image you are editing
    Tool(Document *pDoc);

    // Process mouse events -- this need to be overridden to execute
    // the appropriate behaviour depending on the concrete type of tool
    virtual void OnMouseEvent(const MouseEvent &e) = 0;

    // and so on
};

You then inherit from this abstract tool class to provide the concrete tools your application needs. When a certain tool is selected, you instantiate a corresponding tool object and remember it somewhere. The window forwards mouse events to the currently active tool object, which then does the appropriate thing for the tool that was selected.

This is an implementation of the State pattern that Johannes refers to.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签