14.2. Creating the ModelLocator

The ModelLocator class, unlike the FrontController and the ServiceLocator, is not required to be in the main application file in order for other classes to work, but you will need it for many of the classes that you will create later.

Create a class named FlexBlogModel in the com.FlexBlog.models package. (See Chapter 4 for specific instructions with screenshots.) Edit the class to look as follows:

package com.FlexBlog.models
{
    [Bindable]
    public class FlexBlogModel
    {
        private static var instance:FlexBlogModel = null;
        public function FlexBlogModel(se:SingletonEnforcer)
        {
        }
        public static function getInstance():FlexBlogModel{
          if (FlexBlogModel.instance == null){
              FlexBlogModel.instance = new FlexBlogModel(new SingletonEnforcer());
            }
            return FlexBlogModel.instance;
        }
    }
}
class SingletonEnforcer{};

Make sure that you have added the Bindable metatag before the class definition. This allows other classes to bind to properties on the model.

Of all the singletons used in the Cairngorm framework, the ModelLocator is the only one that you need to implement as a singleton on your own.

For the singleton implementation, make sure that you have:

  • Created a private static variable named instance that will hold the single instance of the class

  • Created an internal class named SingletonEnforcer outside of the package definition

  • Created the constructor method to take in a parameter of type SingletonEnforcer

  • Created a public static method named getInstance that creates the ...

Get Professional Cairngorm™ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.