Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Command vs. Notification  (Read 9331 times)
aaaidan
Newbie
*
Posts: 3


View Profile Email
« on: September 14, 2008, 10:18:45 »

Hi,

I just started refactoring a small as3 application with pmvc a few days ago, and I'm really damn impressed with the framework so far. It's quite easy to insert into an existing application, though I can't wait to build a new app from scratch with it. Kudos to the pmvc team!

Anyway, I have an architecture question about the differences between Notifications and Commands, since there seems to be many instances where both could be used for the same purpose, especially where user-interfaces are involved.

I've had a quick search on the forum and through some of the examples but I can't find any definitive recommendations.

As a contrived example, say I have a ControlPanelMediator which listens for specific clicks on a control panel interface. These clicks should toggle visibilities of other Sprites, such as a sidebar. I see at least two ways to do this:
1) ControlPanelMediator sends a TOGGLE_SIDEBAR_COMMAND notification, which solely causes execution of the ToggleSidebarCommand. This command retrieves the SidebarMediator from the façade, and calls its toggleVisibility() method directly. That method toggles the actual sidebar Sprite's visibilty.
2) ControlPanelMediator sends a SIDEBAR_TOGGLE_BUTTON_CLICKED notification. The SidebarMediator is listening for this Notification, and toggles the sidebar Sprite's visibility in response to this.

In short, #1 is more "command oriented", and #2 is "interface oriented".

My hunch was to go with #1 because it keeps the details of the user interface (clicking, etc) out of the core application code, but is slightly more complicated to implement and maintain. It generally requires the creation of a command name string and a command class for each operation. Similar Commands could perhaps be coalesced into a single class by acting on the body of the Notification, I guess, but #2 is definitely a simpler way to go because it only requires a couple of lines of code.

So basically, is there any recommendation or rule of thumb when deciding whether to use commands vs. notifications? Should commands generally be reserved for changing state? Perhaps things that you want to be 'undoable' should be commands?

Any thoughts, or links, anyone? (Sorry if this post is a bit jumbled!)
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: September 15, 2008, 04:55:28 »

Sounds like your question is really whther to use an interested mediator or a command to respond to any given notification.

The answer is whichever uses less code. The mediator sending the notification doesn't know or care either way. Thus, why involve a command if you don't have to?

-=Cliff>
Logged
aaaidan
Newbie
*
Posts: 3


View Profile Email
« Reply #2 on: September 15, 2008, 03:12:35 »

Yes, that's pretty much my question. :) Thanks, that makes sense.

I think when it comes down to it, I was just trying to get my head around what commands should be used for. I guess working on a non-sandbox project might make this clearer...
Logged
Tehk
Newbie
*
Posts: 4


View Profile Email
« Reply #3 on: April 26, 2010, 02:31:01 »

i never let mediators react to notifications on their own, because it gets a nightmare to keep up with. with bigger projects, esp. with more than 1 developer it gets impossible to know what certain notifications do if they are not clearly mapped to commands.

or is there anything i am missing about how to keep track of those notification-interests/vs. commands-called ?

i really like the idea of going with the "less code" answer, but i dont think that would work for big/multi-dev projects?
Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #4 on: April 27, 2010, 01:10:35 »

1) ControlPanelMediator sends a TOGGLE_SIDEBAR_COMMAND notification, which solely causes execution of the ToggleSidebarCommand. This command retrieves the SidebarMediator from the façade, and calls its toggleVisibility() method directly. That method toggles the actual sidebar Sprite's visibilty.
2) ControlPanelMediator sends a SIDEBAR_TOGGLE_BUTTON_CLICKED notification. The SidebarMediator is listening for this Notification, and toggles the sidebar Sprite's visibility in response to this.
Any thoughts, or links, anyone? (Sorry if this post is a bit jumbled!)

Number 1 feels natural, and I have done it myself, but it is not correct.
Number 2 is better.
Your mediators should not expose public methods to be called directly by commands or other mediators.  Rather, they should react to notifications.  If you modify 1 to do this then you would end up with a command that dispatches a notification to a Mediator, needlessly increasing the number of notifications.  So I recommend 2 because it cuts out the Command middle-man.  If you have more complicated business logic involved (such as re-querying proxies, etc.) then the modified 1 option (ie using a Command to modify visibility via notification) makes sense.

edit:
here's the relevant link
http://forums.puremvc.org/index.php?topic=1698.msg7703#msg7703
Logged
Pages: [1]
Print