4 Replies Latest reply on Apr 6, 2008 12:54 PM by slaingod

    Does Cairngorm Overly Rely on/Promote Singletons?

    slaingod Level 1
      It seems to me that Cairngorm overly promotes the use of Singleton classes for application development, which can cause serious headaches down the road.

      Two of their main design patterns are the CairngormEventDispatcher/CairgormEvent and the ModelLocator, along with the FrontController. I've come across a number of cases where the original design of an application was assumed to be standalone, when in fact a little foresight would have made it clear that ultimately this was an embeddable custom component more than anything. Being able to use it standalone was useful, but in general it would be embedded as a module in another Flex application.

      The problem comes in when you need to add a second instance of the Cairngorm app. I have a creation widget and a playback widget, both built from shared code since the creation widget also includes the playback widget. So now I want to embed BOTH a creation widget AND a playback widget into my application. In fact, just like any other component (think combobox), I might want to include several playback widgets, each showing some particular users creation, all on the same page.

      However, because my playback widget uses Cairngorm, I am pretty much out of luck with the current code base. Assuming I shoehorn multiple playback widgets in somehow (after dealing with all of the service starting/FrontController issue) I have no way to control which of my multiple widgets responds when I hit the play button in one of them, since they are all listening for that event. Similarly my the ModelLocator pattern completely breaks down, since all of the playback widgets will share the same ModelLocator.

      Is this just me? Am I missing something? Or is it that the original designer of this app should known better how it was going to be used a year later? Or is it a breakdown of the Cairngorm design patterns altogether?
        • 1. Re: Does Cairngorm Overly Rely on/Promote Singletons?
          ntsiii Level 3
          It is just a pattern / framework. It is not suitable for all applications, no single framework is. Google it if you want to read endless debate. Or dump it if it is not right for you.

          I have never used it myself.

          • 2. Does Cairngorm Overly Rely on/Promote Singletons?
            slaingod Level 1
            Lol, that's the problem. I have to support an existing Cairngorm application of 50,000 lines with 100 events and 400 listeners.'Dumping it' is going to be a huge problem, and should be a warning to anyone else out there. It would be nice if the framework took into account this common pattern, since it seems to be the most common of the frameworks used. And it seems that the framework documentation doesn't make this glaring problem apparent with phrases like 'all events should be Cairngorm events', 'all you data should be in the singleton model locator', etc.

            I guess my point is, I think that some of these MVC frameworks need to put in big red letters at the top of their docs: 'DON'T USE THIS FRAMEWORK IF IT IS POSSIBILITY THAT YOU WILL EVER WANT TO USE THIS MUTLIPLE TIMES IN A SINGLE SWF.' Maybe that doesn't happen as much as I think. But I've run across it on 2 projects already (of say 10 I've worked on).

            Again, that's unless I am completely missing the big picture and there is some one line fix that solves all of those problems :P Which was my point of posting. Hoping someone knew a quick and dirty way of fixing this.

            That said, there is a way around this, which is to use SWFLoader to load the FlexApp and then use SystemManager to get a pointer to the main application so you can call public methods, etc. But this is a maintenence nightmare, as the embedded fonts/style have to then be put into the 'embedder' apps stage, etc., and you lose strict typing and flexibility in general, and it can cause Design Mode to break in Flex Builder.

            /rant off
            • 3. Does Cairngorm Overly Rely on/Promote Singletons?
              Ansury Level 3
              Yeah, it sounds like Cairngorm may not have been the best choice for the "widgets" you have. I guess you have a point that they should clarify better when you DON'T use Cairngorm. I've never tried to use it when developing a custom component (or anything) if it was going to be added into a full app.

              From reading various articles I get the impression (IIRC) that the guys from Farata Systems (might just be Yakov) don't seem to be very keen on frameworks (Cairngorm is called a 'micro-architecture'), but rather focus simply on strong "component design" and using Flex's event system (i.e. dispatchEvent() ). I definitely appreciate that questioning attitude because too often in IT it seems that people just seem to accept "best practices" and "the way it's 'supposed' to be done" as bible regardless of whether it's really adding any value, or adding needless complexity. I think the answer to everything always "depends" and we have to use our common sense and intelligence to figure out whether we're really gaining anything by doing something "standard" that has questionable value.

              I've used Cairngorm architecture but not run into your problem, maybe because I only use it at an "Application" level, never ever intending to have the product of that project embedded in anything.

              (Haven't read all of this, or I forgot if I did, just putting it here in case something I said about others' opinions wasn't entirely accurate.)
              • 4. Re: Does Cairngorm Overly Rely on/Promote Singletons?
                slaingod Level 1
                Great article. Thanks for posting it.

                Yea, my 'widget' is a video mixer which creates an xml playlist, and the playback component reads in the playlist and plays it back. The code overlap between the two is about 50% as the mixer by its nature contains a player. At the time we were under a serious time crunch (1 month from design to implementation) so some design ideas weren't fully explored, so to speak, and I was just learning Flex at the time.

                That said I do feel there is some use for compartmentalized events, just not completley Singleton. I find myself using this.parentApplication a lot of times when I do need a Singleton, though that doesn't work with as3 classes. It would be interesting to see if there was a way to use, say, namespaces to provide this 'singleton-like' compartmentalization for multiple instances. You might be able to set the namespace before starting to manipulate your component, and your events would determine which namespace they were using automatically and create event names based off of that ( "STATUS_<namespace>") automatically.

                TIme to go build my own framework! lol