Question

Is something like this possible in Actionscript?

Java:

URLFetcherFactory.setCreator(
    new IURLFetcherCreator() {
        public IURLFetcher create() {
            return new URLFetcher();
        }
    }
);

Actionscript:

?

I've been wondering about this and have been unable to find anything that indicates it's possible. Figured if it was possible, I'd be able to find an answer here. Thanks! Stackoverflow rocks!

Was it helpful?

Solution

You cannot create an instance of an interface. You can, however, create a factory class:

public class URLFetcherCreator : IURLFetcherCreator {
    private var _cls : Class;

    public URLFetcherCreator(Class cls) {
        this._cls = cls;
    }

    public function create() : IURLFetcher
    {
        return new cls();
    }
}

Alternatively, change setCreator to accept a Function that returns an IURLFetcher:

URLFetcherFactory.setCreator(
    function() : IURLFetcher {
        return new URLFetcher();
    }
);

OTHER TIPS

Try this:

URLFetcherFactory.setCreator(
    new IURLFetcherCreator() {
            public function create():IURLFetcher  {
                    return new URLFetcher();
            }
    }
);

You can't use anonymous inner classes in AS3. For special cases like callbacks you can use Function instead of anonymous inner classes.

Java:

interface Callback {
     void done(String info);
}
class Service {
     void process(Callback callback);
}
...
myService.process(new Callback() {
     void done(String info) {
          // trace(info);
     }
}

AS3:

class Service {
     public function process(callback:Function):void;
}
...
myService.process(function(info:String):void {
     trace(info);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top