gpt4 book ai didi

apache-flex - 我应该在 Flex 应用程序中的什么位置存储重复使用的静态字符串常量?

转载 作者:行者123 更新时间:2023-12-01 15:32:25 24 4
gpt4 key购买 nike

我有两个共享许多类的 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)
...

最佳答案

我会说按逻辑意义组织常量,而不是单个常量类。

假设您有 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.* 值”,而不是说“返回 SUCCESS、FAILURE、RUNNING...”。

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

关于apache-flex - 我应该在 Flex 应用程序中的什么位置存储重复使用的静态字符串常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/976579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com