tags:

views:

34

answers:

3

Let's say I have an interface

public interface IFoo {
   ...
}

and I have several implementing classes

public class Foo implements IFoo {
    ...
}

...

public class Bar implements IFoo {
    ...
}

...

public class Baz implements IFoo {
    ...
}

I want to reference IFoo in MXML like this

<ns:IFoo id="myfoo"/>

and have it be instantiated at runtime by a factory.

However, the compiler won't let me do this-- it's trying to do "new IFoo" in the generated ActionScript.

How to get around this? How can I use an interface and a factory purely in MXML?

A: 

You can implement interfaces in MXML components with the implements="IFoo" attribute in the component's root node.

Edit:

Sorry, I missunderstood your question. I don't know a way to implement a factory in pure mxml. I guess you have to use Actionscript or mxml states to achieve a similar behaviour.

softcr
I don't want to create a component that implements an interface. I want to *instantiate* a component that implements an interface.
paleozogt
A: 

Check out ClassFactory. It is how things like item renderers are instantiated.

James Ward
A: 

Declaring an MXML child instantiates an object of that type - you can't simply declare a property in MXML without associating an instance with it.

Given that - there's no way to achieve the equivalent of

public var myFoo:IFoo;

in your MXML.

As James pointed out, you can use a ClassFactory to acheive the following:

<mx:ClassFactory class="{Foo}" id="fooFactory" />

but you would be required to call fooFactory.newInstance() to get an IFoo.

Marty Pitt
arg, too bad. :(
paleozogt