views:

745

answers:

1

I have two Cairngorm MVC Flex applications (a full version and lite version of the same app) that share many Classes. I have put these Classes into a Flex Library Project that compiles as an SWC. Both applications use some static String constants. Right now, I am storing these in the ModelLocator:

package model
{
    [Bindable]
    public class ModelLocator
    {
        public static var __instance:ModelLocator = null;

        public static const SUCCESS:String = "success";

        public static const FAILURE:String = "failure";

        public static const RUNNING:String = "running";

        ...
    }
}

This just doesn't seem like the best place to store these constants, especially now that they are used by both applications, and I have setup each application to have its own ModelLocator Class. Plus this is not the purpose of the ModelLocator Class.

What would be a good way to store these constants in my shared Library?

Should I just create a Class like this?:

package
{
    [Bindable]
    public class Constants
    {
        public static const SUCCESS:String = "success";

        public static const FAILURE:String = "failure";

        public static const RUNNING:String = "running";
    }
}

and then reference it like this:

if (value == Constant.SUCCESS)
    ...
+8  A: 

I'd say organize the constants by logical meaning, instead of a single Constants class.

Say you have the 3 you show as some kind of task state, and you have some more that are used as error codes for file access (just making stuff up here):

public class TaskStates {
  public static const SUCCESS:String = "success";
  public static const FAILURE:String = "failure";
  public static const RUNNING:String = "running";
}

public class FileErrors  {
  public static const FILE_NOT_FOUND:String = "filenotfound";
  public static const INVALID_FORMAT:String = "invalidformat";
  public static const READ_ONLY:String = "readonly";
}

I find this makes it easier to document what the expected values are for something. Instead of saying "Returns either SUCCESS, FAILURE, RUNNING, ...", you can just say "Returns one of the TaskState.* values).

You could put all these in a single package for constants, or you could have the constant classes live in the same package as the classes that use them.

Herms
Good ideas. Thanks.
Eric Belair