I'm in a bit of a tricky situation where I need to use the Observer pattern but I don't really know the best way to go about it.

Here's a quick briefing on my application:

I'm implementing a GUI application that allows users to create flowcharts and mindmaps by dropping shapes onto a canvas and then manipulating the shapes by clicking, dragging, and holding. My shapes have the base class type of MindMapComponentInstance, and my canvas has type Canvas. The Canvas object acts as a kind of controller, creating and storing references to all the MindMapComponentInstance objects when a user drops onto the canvas.

Now I need to implement mouse event functionality. Specifically, I need the Canvas object to "watch" or "listen" to all the mouse events that are registered on the MindMapComponentInstance objects. So, basically I have an undefined amount of publishers (the MindMapComponentInstance objects), and one subscriber (the Canvas object).

To make things slightly more complicated, I need to be able to distinguish between different type of mouse events. A click, drag, hold, etc must all be distinguished by the Canvas object, as it needs to act differently depending on the type of mouse event that is registered by the MindMapComponentInstance objects.

What is the best way to implement what I need? Will the standard observer pattern suffice? Or should I do things a little differently to result in better code design?

EDIT: I thought I'd mentioned that my application is a web app and as such is being written in javascript, if that helps.

1 Answer 1


Observer will work fine here. You can distinguish between type of clicks with the event parameter that you pass.

What concerns me is that it sounds like Canvas is trying to be model, view, and controller all in one. Canvas should have a single responsibility. If Canvas stores instances of MindMapComponentInstance it's a model. If it listens as the user manipulates them it's a controller. If it translates the model to a way to present it's state it's a view. If it does all three it's a big ball of mud that needs to be broken up.

There are many ways to implement MVC and many variations of it. Here's one I find interesting.

The observer pattern is just a small part of designing a good architecture. Don't expect simply adding it to fix everything.

  • Thank you for your reply. I think you make a good point about the Canvas object. Canvas is basically acting like a controller and a model. Within the Canvas is another object that acts as the view, i.e. does all the rendering and storage of the MindMapComponentInstance objects, but the Canvas also needs to manipulate them, as my question refers to. Another thing I'm worried is that I have a parent object observing it's children, which seems a little "off" to me.
    – PythonNewb
    Jan 9, 2017 at 5:06
  • 2
    Nothing wrong with "parents" observing "children" so long as parents don't have to know childish details. The model doesn't care if it's a image or a text box. It knows it's moving to 10,15. What it will look like when it gets there is something else's problem. Jan 9, 2017 at 5:13

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.