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: Static method or Command?  (Read 7769 times)
Engel384
Newbie
*
Posts: 2


View Profile Email
« on: April 26, 2011, 06:05:22 »

I'm wondering, in which cases to use a static method or when to use a Command in PureMVC? I allways use Commands if the system should react on an user-input from the View, but assumed there will be a function which will be called repeatedly from many Commands. Should this function also be a Command and should be called via a Notification from a Command or is it better to create a static method anywhere? The great disadvantages to the first are, that there is no reasonable Exception-Handling possible and Commands have no return value. The great disadvantage to the second is, that the static methods mostly need the Facade which I must pass then to the respective method...

So how do you solve such problems?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: April 26, 2011, 10:03:17 »

I'm wondering, in which cases to use a static method or when to use a Command in PureMVC?

Static methods have their place, but usually within a class for its own use. I never use static methods if there is a way to avoid it. It is the sort of tight-coupling that MVC is intended to provide an alternative to. If I'm integrating with a third party library that implements them, I'll still mediate the offending class and only allow my application to communicate with it via the Mediator.

there is no reasonable Exception-Handling possible and Commands have no return value.
Certainly you can handle exceptions inside commands, Try/catch works as well in a command as anywhere else. You can send notes off to trigger other commands based on the trapping of errors or the sucessful completion of the command.

A case in point, I just implemented this in a client's application. I handling all fatal errors that can occur during a very complex startup by triggering a command that sends a Mediator a note creates the error display component (not just an Alert), with the error, and the mediator displays the error in its view component (a viewstack) by injecting this error component and displaying it. The Mediator had to be registered up front before the rest of the view was complete, but otherwise this was a straightforward thing to deal with.

there will be a function which will be called repeatedly from many Commands.
Then why not make an abstract command subclass that has this method and have all your concrete commands that need this method subclass your abstract command?

-=Cliff>
Logged
Engel384
Newbie
*
Posts: 2


View Profile Email
« Reply #2 on: April 27, 2011, 02:05:06 »

Thanks for the fast reply!

Static methods have their place, but usually within a class for its own use. I never use static methods if there is a way to avoid it. It is the sort of tight-coupling that MVC is intended to provide an alternative to. If I'm integrating with a third party library that implements them, I'll still mediate the offending class and only allow my application to communicate with it via the Mediator.

I'm sorry...I actually meant Singleton-Classes, but that's also not very nice...

Certainly you can handle exceptions inside commands, Try/catch works as well in a command as anywhere else.

Yes, that's right, but I meant the complexer Exception-Handling with "Bubbling" the Exception through recursive Commands.

Anyway, I want to try it again with your tips. Thank you!
Logged
Pages: [1]
Print