Question

All is in the question, why menus are commonly implemented with the Command Design Pattern and not with the Observer pattern ?

Was it helpful?

Solution

There are two aspects, the "dispatching", an option is selected from a menu or a button is clicked and some code is run. Then there's the actual code that's run.

I don't know which UI framework you are referring to but I'd expect to see both Observer and Command patters used in both cases. I wonder if what's happening is that the Menu case and the Button case are just implemented by your framework in ways that make different aspects visible.

So for menus, you provide different command objects, you never actually see the internal dispatching that causes your command to be invoked. My guess is under the covers some Event handling must be happening, so the Observer pattern might well be in use, it's just you don't see it.

In the button case the code that's invoked can be thought of as a command object but we more explicitly wire it to an event so we first see the Observer pattern.

OTHER TIPS

I can see one use case for using observer pattern with menu Item or button. Assume there is Horoscope calculator. User has a button to start horoscope generation. User will click the button and command issued to server for calculation. Later same button can observe server status or vice versa for status updates or to display print and the fire print command from the button. So Observer : Events needs to dispatched. Command: an action performed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top