在 Flex MVC 环境中重用功能的最佳方式是什么?

发布于 2024-07-11 17:43:41 字数 115 浏览 8 评论 0原文

我当前的项目使用 Cairngorm MVC 架构。

我有几个命令使用相同类型的返回值的函数。 我希望将此函数放在一个地方,并重用它,而不是在每个命令中重复代码。 做这个的最好方式是什么?

I am using a Cairngorm MVC architecture for my current project.

I have several commands which use the same type of function that returns a value. I would like to have this function in one place, and reuse it, rather than duplicate the code in each command. What is the best way to do this?

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

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

发布评论

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

评论(3

洒一地阳光 2024-07-18 17:43:41

在 Cairngorm 类之一中创建静态类或静态方法。

class MyStatic
{
    public static function myFunction(value:String):String
    {
        return "Returning " + value;
    }
}

然后你想在哪里使用你的函数:

import MyStatic;

var str:String = MyStatic.myFunction("test");

另一个选择是创建一个顶级函数(a la“trace”)。 查看我写的这篇文章 在这里

Create a static class or static method in one of your Cairngorm classes.

class MyStatic
{
    public static function myFunction(value:String):String
    {
        return "Returning " + value;
    }
}

Then where you want to use your function:

import MyStatic;

var str:String = MyStatic.myFunction("test");

Another option is to create a top level function (a la "trace"). Check out this post I wrote here.

禾厶谷欠 2024-07-18 17:43:41

这里有很多选择 - 在模型或控制器中公开定义的函数,例如:

var mySharedFunction:Function = function():void
{
   trace("foo");
}

...新的或现有的类的静态方法等。不过,最佳实践可能取决于函数需要执行的操作。 你能详细说明一下吗?

You have lots of options here -- publicly defined functions in your model or controller, such as:

var mySharedFunction:Function = function():void
{
   trace("foo");
}

... static methods on new or existing classes, etc. Best practice probably depends on what the function needs to do, though. Can you elaborate?

甜味超标? 2024-07-18 17:43:41

为您的命令创建一个抽象基类,并将您的函数添加到受保护的范围中。 如果您需要在其他地方重用它,请将其重构为实用程序类上的公共静态方法。

Create an abstract base class for your commands and add your function in the protected scope. If you need to reuse it anywhere else, refactor it into a public static method on a utility class.

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