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: Have I got too many Singletons?  (Read 6813 times)
littleboy147
Newbie
*
Posts: 2


View Profile Email
« on: May 05, 2010, 10:31:52 »

I've started realising the power of multicore but, having used it in anger a couple of times, certain limitations in my approach have come to light.

I know that all my cores will require a stylesheet and a library. My approach thus far has been to load these dependencies in the shell core and pass them to my child cores using the interface method outlined here:

http://www.as3dp.com/2009/07/22/a-non-flex-actionscript-example-of-a-puremvc-multicore-application/

There are a few things which I don't like about this approach.

  • It's quite a tedious process passing these dependencies through the document class, the facade, the start up command, and finally setting them on the core's Proxy. 
  • It doesn't feel right accessing the library and stylesheet from a Proxy.
  • The method of accessing these dependencies differs between the shell and child cores.

To that end I had decided to handle my library and stylesheets as multitons. It seems to me that this approach would solve the issues outlined above and also make it very easy for me to define a separate library and stylesheet for a particular core should I need to. I already handle my Dictionary and Settings in this way.

My concern is with the (growing) number of dependencies which are accessible as singletons or via static methods. In my case these are:

  • Dictionary (multiton)
  • Settings (multiton)
  • StyleSheet (multiton)
  • Library (multiton)
  • Logger (static factory)
  • LayoutManager (multiton)

Admittedly, the addition of two doesn't make a massive difference to the total, but it has made me think.

I'd be lying if I said that we did much (any) test driven development or unit testing, and it might be that I'm worrying about problems I may never encounter, however I am conscious enough of the negative implications of rampant singleton use to be concerned.

Given that the (house) rule is that these dependencies should only ever be accessed via the static methods within the boundaries of the framework, do you think I'm setting myself up for problems in the future with this approach? Have I got too many singletons? If so, what can I do to improve my approach? I have considered using a ServiceLocator but lack of familiarity with it as a pattern has put me off diving in.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: May 08, 2010, 12:15:35 »

Consider placing the responsibility for retrieving the styles and other resources in a core unto itself. Have the shell plumb the cores such that all cores needing resources have bidirectional pipe communications.

Once plumbed, you can. Handle access of those resources from separate cores in a few ways.

You could have the shell send it a message then have it broadcast (via tee'd standard out) the resources to the waiting cores (automatic dependency injection).

Or you can plumb them and then have each module request its own resources by sending a message to the resource core and waiting for a message. In this case the resource core needs to know what pipe to output a response on. Name th output pipes the same as the core's multiton key and send that on the request message. Then the resource core responds on the output pipe by that name.

Of course you can mix and match the above approaches.
 
-=Cliff>
Logged
littleboy147
Newbie
*
Posts: 2


View Profile Email
« Reply #2 on: May 28, 2010, 10:01:02 »

Thanks for this Cliff, and thanks for all your hard work on the framework.

I'll make some time to investigate pipes with your suggestions for the handling of dependencies in mind.
Logged
Pages: [1]
Print