Getting Started with Cairngorm – Part 1
October 7th, 2007
I spoke about Cairngorm 2.2 in the Flex Bootcamp at Max this week. Many people were interested in Cairngorm, but I only had about 10 minutes to explain the basics of Cairngorm. I guess the easiest way to assist these people is to do a quick blog series on the benefits of Cairngorm. This series will combine articles with "code-along" videos.
Disclaimer: I do not claim the be "the expert" on Cairngorm - I am far from it. However, I have used Cairngorm on several large projects (both at Georgia Tech and in my own business). I am certainly open to corrections if you see that I have made an error on this project. If you want "the experts" check out: Steven Webster, Alistair McLeod, Alex Uhlmann, and Peter Martin.
Note: The guys at Adobe Consulting (that developed Cairngorm) are currently investigating some new things with the framework as a whole. It is possible (actually probable) that some of these things will change in the future. One of the very specific areas of change is the Model Locator.
Part 1 – Getting Started By Using a Model Locator
The Model Locator pattern is used in Cairngorm, but you don't have to have a full Cairngorm Implementation to use the pattern. First, let's cover what benefits that you get from using a Model Locator.
A Model Locator is a centralized repository for all of the data that is needed across your application. Your data will exist inside of a "singleton class". This "class" can only have one instance of itself. Why is this important? Let me give you an example.
We have great mini-notepads at work that I use to write down data while I work. However, sometimes, I lose a notepad – get a new one, and then find the old one. After both have been heavily used – it is really hard to find out which notepad I used to write down a piece of information from one week ago. That is a simple example – but imagine if I had 20 notepads? This could get crazy.
In the same way you could have a "class" that gets "instantiated" 20 times within your application (even if you don't mean for it to). The easiest way to eliminate this problem is to use a "singleton". A singleton is a class that is never "created" in the traditional way. It has one main rule – no more than one of itself can exist at any point in time. How does it do this? I will show you in the Model Locator example.
-
package net.davidtucker.CairngormSample.model {
-
-
import com.adobe.cairngorm.model.IModelLocator;
-
-
[Bindable]
-
public class ModelLocator implements IModelLocator {
-
-
// Single Instance of Our ModelLocator
-
private static var instance:ModelLocator;
-
-
public function ModelLocator(enforcer:SingletonEnforcer) {
-
-
if (enforcer == null) {
-
throw new Error( "You Can Only Have One ModelLocator" );
-
}
-
}
-
-
// Returns the Single Instance
-
public static function getInstance() : ModelLocator {
-
-
if (instance == null) {
-
instance = new ModelLocator( new SingletonEnforcer );
-
}
-
return instance;
-
}
-
-
//DEFINE YOUR VARIABLES HERE
-
-
}
-
}
-
-
// Utility Class to Deny Access to Constructor
-
class SingletonEnforcer {}
This code may look a bit daunting in the beginning, but trust me that it is not as difficult as it may appear. First, we have our package definition and we import some classes. Right now we know that we will need the IModelLocator interface. To use this you will need the Cairngorm SWC that can be found here: Cairngorm. Also note - you could build a model locator without Cairngorm, and I do this frequently on small projects (you just leave out the 'implements IModelLocator' and 'import com.adobe.cairngorm.model.IModelLocator' code from the Model Locator).
-
[Bindable]
-
public class ModelLocator implements IModelLocator {
-
-
// Single Instance of Our ModelLocator
-
private static var instance:ModelLocator;
Next we have our class definition. It is important that you use the Bindable metatag directly above your class definition. This will allow all of our variables that we define inside of the Model Locator to be used for binding. We also will go ahead and create one variable. It will be called "instance" and it will be of type ModelLocator. This will be the variable where we will store our one instance of our class. It will also be denoted as a "static" property. If you are not sure what a "static" property is, it's ok - we will discuss that in our next lesson.
-
public function ModelLocator(enforcer:SingletonEnforcer) {
-
if (enforcer == null) {
-
throw new Error( "You Can Only Have One ModelLocator" );
-
}
-
}
This is followed by the constructor. The constructor takes on argument - "enforcer". You will notice that this "enforcer" has a type of "SingletonEnforcer" which is defined directly after our class. Here is the logic behind that:
- When you put a class in an Actionscript file below the main class, it is only available to that class. Many people refer to these as "utility classes" (although many people use that term in a much broader scope).
- If the constructor requires this argument - then only our main class can create an instance of itself, because we do not have access to the "SingletonEnforcer" class - only the main class has this access.
- We will not access our class in the normal way by using the "new" statement because we can't call the constructor (I will show you how we will do it in a bit).
Once we get inside of the constructor, we have a few lines that make sure things work as planned. The "if" statement ensures that we had a valid "enforcer" passed in. If there wasn't it throws an Error stating that "You Can Have Only One ModelLocator".
-
// Returns the Single Instance
-
public static function getInstance() : ModelLocator {
-
if (instance == null) {
-
instance = new ModelLocator( new SingletonEnforcer );
-
}
-
return instance;
-
}
The "getInstance" function is how we will access our ModelLocator from our application. This function simply passes back the "instance" of the class. If it doesn't exist yet, it creates it. We can now get the ModelLocator by using the following code:
-
var model:ModelLocator = ModelLocator.getInstance();
Video Example - Getting Started and Building a Contact List
Application Code
Download (418 kB)




36 comments on “Getting Started with Cairngorm – Part 1”
01
Great video tutorial, but what about the warnings I get?
“Data binding will not be able to detect assignments to “modelLocator” “.
02
There must have been one item I left out:
When you get the “instance” of your modelLocator - you need to make that variable Bindable as well. The binding will still work without it (since the entire Model Locator is bindable, but it will throw warnings unless you add the following code):
[Bindable]
private var modelLocator:SampleModelLocator = SampleModelLocator.getInstance();
That should fix the warnings. Thanks.
03
Hehe, should have known that. When can we expect a new tutorial?
04
Neil Webb did a great series of posts on a introduction to Cairngorm
http://nwebb.co.uk/blog/?cat=13
My advice would be not to follow Cairngorm too strictly. Take the good things out of it for each particular project your working on.
05
The next tutorial should be out in a few days. It will be on “Binding the View to the Model Locator”.
06
hi folks.
Great artical mate.
Just wondering, does anyone know of an eclipse (or anything else in fact) tool for automatically creating VOs from MySQL tables?
It’s just such a pain of a jop having to copy it all by hand, and also another place where errors can creep in.
In fact it wouldn’t even need to creat propper actionScript code, just something like it that I could then change into the propper format.
The project I’m currentl;y working on is a recoding of a system that was originally written in access and SQL. so the Database is already there (since converted in MySQL) and there are loads of tables and making all the VOs is becomming a real pain.
any ideas anyone?
cheers
glenn.
07
Yes, you can actually generate Actionscript VO’s with the Adobe Coldfusion Extensions for Eclipse - http://www.adobe.com/support/coldfusion/downloads.html . If you aren’t in a Coldfusion environment, this might be a pain.
There is also a CRUD generator for Flex 2 Beta 3 that works with a variety of different languages. I am assuming that it creates VO’s on the client side as well as the server side code. More here: http://www.onflex.org/ted/2007/09/flex-3beta-2-crud-wizard-for-aspnet-php.php
08
There’s also a great tool from Tyler Beck at http://www.tylerbeck.com/CairngormCreator/ for generating stub code for your project.
09
I agree that is a good tool. I don’t believe that it has MySQL Introspection though. We will be covering that tool (as well as CairnGen) in a future tutorial.
10
Why does Anatole Tartakovsky take such exception to the Cairngorm framework?
http://flexblog.faratasystems.com/?p=252
“JB: Moving on other topic, I would like to talk on frameworks. Cairngorm and some other frameworks that I call “shallow” frameworks. They are not necessarily provide a lot of functionality in a sense that Flex frameworks does. It provides what you might call Superpad – a set of interlocking patterns. I wonder if a shallow framework provides cure or actually causes disease by blocking developers from providing solutions and analyzing the problems.
AT: That is exactly the problem we are seeing. People come to us to rewrite Cairngorm projects. Cairngorm is answering to “how” question, but “why” or “what” just is not in the picture at that point. We believe that you should take some patterns – and enhance them on individual basis for a particular application, but the “meat” of the solution is in the developer’s framework. People need to have big pieces of an application available as components, customizable products that can perform common functions and transparently be used in the application. They are tested and give you the functionality that you want right away. You can start with small, single function components like logger or “caching logging” screen David mentioned, or you can try deep waters with complete set of components that very much fill the gaps in the current Flex frameworks. Over the years, we added objects like Report, DataForm, TreeGrid, OlapTreeGrid, etc. We added the functionality that is important for reliability of your applications like robustness layers for communications. Architectural frameworks are very much like skeleton of the application, and Application Framework is actually adds muscle – you can’t have one and not the other.”
11
[...] In Part 1, I discussed the basic implementation and use of the ModelLocator pattern. This pattern is one of [...]
12
First code sample, line 22 you refer to “VizionModelLocator”. Is this a typo? I would have expected it to say this:
instance = new ModelLocator( new SingletonEnforcer );
Am I missing something? ( Quite possibly ).
Great screecast, BTW. What did you use to create it? ( Camtasia? Captivate? Something else? )
13
Jeffry - Yes this was an error (that is what I get for copying/pasting from another project I am currently working on). I have corrected it in the article, and I will correct it in the Application Code download as well.
As for the screencast - I am using Camtasia. It is one of the few situations where I prefer another product over an Adobe product. I feel that overall, it does a better job than Captivate.
14
[...] Teil 1 beschreibt das ModelLocator Pattern [...]
15
[...] Part 1 [...]
16
Hello David,
great video! I’m new with flex and cairngorm, and this video is very usefull
i’m getting an error on this line:
——————–
instance = new SampleModelLocator( new SingletonEnforcer );
——————–
I’m getting the following error:
——————–
Incorrect number of arguments. Expected 0.
——————–
I don’t know what is wrong, do you know what could be wrong
thanks!
17
never mind,
I’ve found it, i wrote something wrong a bit above that line…
18
@Laurens - Great. Please let me know if you have any more problems.
19
Hi, it maybe a stupid question but .. Im follow the tutorial but how do you make eclipse code hint the AS scripts (SampleModelLocator.as) Mine doesnt ..
20
David, this series is amazing!!! Helping me so much to really understand the framework and your explanations hit every single question I have had. Just one thing I noticed, your video uses SampleModelLocator as a naming convention, but your written tutorial uses ModelLocator. Keep up the great work and thanks again for the hard work!
Conrad
21
[...] Part 1 [...]
22
[...] http://www.davidtucker.net/2007/10/07/getting-started-with-cairngorm-%e2%80%93-part-1/ [...]
23
I do not understand the necessity of SingletonEnforcer. If the constructor is declared private it cannot be accessed from anywhere outside the class.
24
@T. Venkat Ram - Actionscript 3 does not have private constructors. This means that you have to create your own method of enforcing the singleton rules.
25
Hi from Spain.
David, great Job. I learn a lot with your tutorials.
I think there is one error (but I am not sure 100%).
In this piece of code (inside the constructor of ModelLocator)
if (enforcer == null) {
throw new Error( “You Can Only Have One ModelLocator” );
}
}
I think the condition (enforcer == null) is wrong. In mi opinion would have to be (enforcer !=null).
It´s ok?
Thanks
26
@Jose - Actually it is correct, but I probably should change the Error message to be more understandable. If you used the method that you just described, the class could not ever be instantiated - because even the class itself could do the instantiation without throwing an error. The Error should read “This Class is a Singleton - please use the getInstance method to return the instance”.
27
Yes, You are right. I was thinking in a wrong way
28
Great tutorial. I am getting myself back into Flex and Cairngorm after a 9 month hiatus and this is helping me refresh my memory really well.
29
Hi David,
I could not stop reading the articles and watching all the videos one after the other to get an overview of the Cairngorm framework for the past one week. Great series. Really appreciate your efforts…
30
Fantastic tutorial David.
Easy to follow and the video just makes such a difference.
Sometimes I get a bit lost in spaghetti code and I hope that by getting a handle on some framework ideas I can structure my code better.
So thanks for your ongoing efforts.
31
Thanks for the awesome tutorials
32
[...] Home Hello World with Cairngorm Singleton Pattern with Flex/Cairngorm Cairngorm Video Documents [?] Share This This entry was written by TJ, posted on April 10, [...]
33
you rock! this was an awesome tutorial! Helped clear a lot of stuff.. thanks so much again!
34
There is an alternative for Cairngorm, called Penne: http://www.flexpasta.com/index.php/2008/04/19/introducing-the-penne-framework-for-flex-3/
35
Wow! Thank you so much for these tutorials.
36
[...] David Tucker - Web Development Goodness » Blog Archive » Getting Started with Cairngorm – Part 1 (tags: flex) [...]
Leave a Reply