views:

292

answers:

3

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?

+1  A: 

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?

Christian Nunciato
+1  A: 

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.

cliff.meyers
+1  A: 

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.

Typeoneerror