AS3: How do I set a method argument to implement multiple interfaces?
The argument to my func () method must implement two unrelated interfaces IFoo and IBar. Is there a better way to do this than to declare another interface just for this purpose that inherits from IFoo and IBar and uses that interface as the argument type?
public interface IFooBar extends IFoo, IBar {
}
public function func(arg:IFooBar):void {
}
I am developing Flash Player 9.
Features of what I am trying to do:
I am writing a small library of classes to help me develop Flash games. To aid in debugging and verifying game results, the library requires the user to break the game into the following components:
- rawInputProducer: generates device input events from any flash input events, saved events from a previous session or other source
- rawInputProcessor: listens for device input and creates custom commands from it
- gameController: listens for custom commands and updates game state.
- gameView: listens for game state changes with gameController and updates the scene
All of these components generate events (called "messages" so as not to be confused with flashes). The class Engine
initiates the components and starts the main loop.
In order for the components to be as versatile as possible, it Engine
only requires that they implement IMessageDispatcher
and ITickable
. I want to write classes that are only IMessageDispatcher
or only ITickable
. I also have a generic MessageDispatcher
one that implements IMessageDispatcher
that my components are likely to expand.
The original question is about how can I declare a method like this:
Engine.setGameController(controller: ITickable & IMessageDispatcher):void
a source to share
Having an interface extends 2 unrelated interfaces so that you can "correctly" enter a parameter of one function / method, it looks like a code smell. You can get away with it, but I think it might be a symptom of a design problem.
I may be wrong as I don't know anything about your specific problem. And I am assuming that both IFoo and IBar are not related, which may or may not be.
Perhaps if you talk a little more about your problem, what those interfaces represent, and how the method in question deals with an object that implements both interfaces, other people can suggest alternatives to your design (if need be). But as a general rule of thumb, if you only do this to keep the compiler happy, chances are that you are doing something wrong.
a source to share