I have more a trouble of thinking how to make it "regular" rather than how to make it work.
As you, it's recommended among almost all the experianced developers to program to interfaces.
Let's just look at an example. I have two interfaces:
IView and
IItemRenderer.
IItemRenderer is an interface for single data object visualization.
IView is like some some of container for item renderers, and it has methods like:
addItemRenderer(rend:IItemRenderer)
removeItemRenderer(rend:IItemRenderer).
Everything looks good for now... We just implement those interface with exact classes and things go well.
My trouble starts when I have to deal with item renderers as not just instances of
IItemRenderer, but as for example instances of
IEventDispatcher. Say I just want to hang some event listener to freshly added renderer.
So what do I do? Pass an exact renderer as an argument to view's addRenderer(). Argument's exact type is some display object. But in fact, I should deal with it as with
IItemRenderer. How is that? I cast the argument to some type, for example Sprite. Eh... This is the problem. Since IMO casting is a very bad thing in this case.
So, what are the ways to go:
1. Just cast it implying on the fact, that it
is an
IEventDispatcher.
2. Let the
IItemRenderer extend
IEventDispatcher of
IUIComponent (well in a Flex case). Ehh... I think extending interfaces is a bad thing. Interface is like some atomic thing keeping one single functionality set. Yes, this one solves thew problem... but what if I need more interfaces to be mixed in? AS3 does not support multi-inheritance. Moreover sdometimes there is no interface to one or another flash class, take a look at Sprite for example. It doesn't have someth like ISprite.
3. The solution I use, but still not sure if this one is a regular one. I make some
AbstractItemRenderer class, that extends UIComponent (or Sprite) and implements
IItemRenderer and
IUIComponent and
IEventDispatcher. After that I change interface
IView so for now it deals with that abstract class, not with the pure interface. This method looks the most handy to me. But is it still a programming to interfaces??? I'm not sure...
What do you guys think of it? Which one is right, and which one is wrong? Which way do you prefer, and why
I'd appreciate any discussion of those, who have the same trouble making their head boilng sometimes...
Sorry for my english