我应该在 Flex 应用程序中的哪里存储重用的静态字符串常量?

发布于 2024-07-24 05:12:06 字数 997 浏览 8 评论 0原文

我有两个共享许多类的 Cairngorm MVC Flex 应用程序(同一应用程序的完整版和精简版)。 我已将这些类放入一个可编译为 SWC 的 Flex 库项目中。 这两个应用程序都使用一些静态字符串常量。 现在,我将它们存储在 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";

        ...
    }
}

这似乎不是存储这些常量的最佳位置,尤其是现在两个应用程序都使用它们,并且我已将每个应用程序设置为拥有自己的 ModelLocator 类。 另外,这不是 ModelLocator 类的目的。

将这些常量存储在我的共享库中的好方法是什么?

我应该创建一个像这样的类吗?:

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

        public static const FAILURE:String = "failure";

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

然后像这样引用它:

if (value == Constant.SUCCESS)
    ...

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)
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拔了角的鹿 2024-07-31 05:12:06

我想说的是按逻辑含义组织常量,而不是单个 Constants 类。

假设您有显示为某种任务状态的 3 个,还有更多用作文件访问的错误代码(只是在这里编造一些东西):

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";
}

我发现这可以更轻松地记录预期值的内容某物。 您可以只说“返回 TaskState.* 值之一),而不是说“返回成功、失败、运行……”。

您可以将所有这些放入常量的单个包中,或者您可以将常量类与使用它们的类位于同一个包中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文