Getting Error when working with ActionScript Class and Interface in FLEX 3, ActionScript 3.0 - actionscript-3

I am currently new in ActionScript and I am creating an Interface and Class where class implements Interface.
I have created Interface and Class both in src/com folder. Here is the code what I did till now.
Interface
package com
{
public interface TestData
{
function getInput(str:String):void
function getOutput():String
}
}
Class
package com
{
public class EntityEL implements TestData
{
public var uname:String;
function getOutput():String
{
return uname;
}
function getInput(str:String):void
{
uname = str;
}
public function EntityEL()
{
}
}
}
mxml file
public var etn:EntityEL = new EntityEL();
public function btnClick():void
{
etn.getInput(value.text);
Alert.show(etn.getOutput());
}
<mx:Button label="Button Click" click="{btnClick();}" />
<mx:TextInput id="value" />
I am getting an error "1044: Interface method getInput in namespace com:TestData not implemented by class com:EntityEL."
Please Help me to solve this problem.

The idea of an interface is to define a contract between the caller and callee objects: Which methods can be accessed, which parameters are required, and what kind of data will be returned.
In order for that contract to make any sense, these methods have to be accessible, so that the "outside world" is allowed to call them.
When you omit access modifiers, the Flex compiler assumes internal as the default, which means that classes from within the same package have permission to access the methods - and so to some extent, this contract seems fulfilled. The same would be true for any other namespace.
Strangely enough, Adobe clearly doesn't allow it: Your method implementations have to be public.
However, you can declare your interface as internal, so that only classes from the package are allowed to implement it, and that leaves a way to keep your API internal, as well - if that was your intent.

Related

Can't call component method from Castle Windsor OnCreate

I'm using Castle Windsor, which generally rocks, however I want it to call a method on my component when it is created and seem to have hit a limitation with OnCreate. For exaxmple:
interface IService1
{
void op1();
}
interface IService2
{
void op2();
}
class MyComponent : IService1, IService2
{
public void Init() // not part of either service
{
}
public void op1()
{
}
public void op2()
{
}
}
// I want to call the component's Init() method when it's created, which isn't part of the service contract
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.Init()));
// I could call something from IService1
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.op1()));
// But I can't call anything from any of the other services
container.Register(Component.For<IService1, IService2>().ImplementedBy<MyComponent>().OnCreate(s => s.op2()));
The first registration won't compile, complaining that it "cannot resolve symbol Init" because the instance passed to the delegate is of type IService1. OnCreate seems a bit limited to me, as in the third case when there are multiple services exposed it only allows you to bind to the first one you declare. I'd have to swap IService1 and IService2 around in order to call op2, but that's just moving the problem around.
Why isn't the type passed in the delegate that of the component being registered? Then I'd be free to call whatever method I like. Is there a way around this? Assume I can't put the Init() code in the component's constructor.
Don't be constrained by the strongly typed nature of C#
Yes, the way the API is constructed it's based off of the first service of the component but you can always cast it down to its actual type (or a secondary service)
.OnCreate(s => ((MyComponent)s).Init())
Alternatively, implement Castle.Core.IInitializable or System.ComponentModel.ISupportInitialize (if you don't want your components to reference Windsor) and then you won't need .OnCreate() at all.
For future reference, here's the relevant documentation.

Any alternative to injecting Castle Windsor typed factories?

Most of my components are registered using the code-based (fluent) approach, but there is one particular component that I need to resolve differently at runtime. This is the interface and a couple of concrete implementations:-
public interface ICommsService ...
public class SerialCommsService : ICommsService ...
public class TcpCommsService : ICommsService ...
Some of our users will need the serial service while others will need the TCP service. My current solution (which works btw) is to use a typed factory and a custom component selector - the latter reads an app.config setting to determine which implementation the typed factory will resolve and return.
First the typed factory (nothing special about this):-
public interface ICommsServiceFactory
{
ICommsService Create();
void Release(ICommsService component);
}
Next, the custom component selector, which reads the fully-qualified type name from app.config (e.g. "MyApp.SomeNamespace.TcpCommsService"):-
public class CommsFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(MethodInfo method, object[] arguments)
{
return ConfigurationManager.AppSettings["commsServiceType"];
}
}
Then the registration stuff:-
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactoryComponentSelector>()
.ImplementedBy<CommsFactoryComponentSelector>());
container.Register(Component.For<ICommsFactory>()
.AsFactory(o => o.SelectedWith<CommsFactoryComponentSelector>()));
container.Register(Component.For<ICommsService>()
.ImplementedBy<SerialCommsService>().LifeStyle.Singleton);
container.Register(Component.For<ICommsService>()
.ImplementedBy<TcpCommsService>().LifeStyle.Singleton);
Finally, an example class with a dependency on ICommsService:-
public class Test
{
public Test(ICommsFactory commsFactory)
{
var commsService = commsFactory.Create();
...
}
}
As already mentioned, the above solution does work, but I don't like having to inject the factory. It would be more intuitive if I could just inject an ICommsService, and let something somewhere figure out which implementation to resolve and inject - similar to what I'm doing now but earlier in Windsor's "resolving pipeline". Is something like that possible?
You can use UsingFactoryMethod here:
container.Register(Component.For<ICommsService>().UsingFactoryMethod(kernel => kernel.Resolve<ICommsServiceFactory>().Create()));
You can inject ICommsService to any class now. ICommsServiceFactory can be a simple interface now:
interface ICommsServiceFactory
{
ICommsService Create();
}

Super interface and super class having the same method name

I am trying to create a spark datagrid item renderer. This item renderer extends a checkbox, and implements IGridItemRenderer
public class CellCheckBoxItemRenderer extends CheckBox implements IGridItemRenderer
When I implement IGridItemRenderer, I need to implement the interface methods, I am having a problem with the following methods:
public function get hovered():Boolean
{
}
public function set hovered(value:Boolean):void
{
}
since the methods are inherited as well from the checkbox
EDIT
The signatures of the functions
//spark checkbox signature
protected function get hovered():Boolean
protected function set hovered(value:Boolean):void
and the signature above belongs to the interface IGridItemRenderer
I guess the implementation of IGridItemRenderer is the more important part, so you can use it in a datagrid. The CheckBox provides just the functionality, you don't have to extend it if there are conflicts in my opinion.
public class CellCheckBoxItemRenderer implements IGridItemRenderer {
private var checkBox:CheckBox;
public function getCheckBox {
return checkBox;
}
//...
}
If CheckBox would implement any useful interfaces, you could also implement them in your renderer and delegate the methods to the checkbox, which may let you encapsulate the whole checkbox. That's not the case here though.
The problem is that interfaces, by design, only specify the signature for public functions, whereas the function in Checkbox is set as protected.
The only solutions:
remove the interface/Checkbox class from CellCheckBoxItemRenderer
remove the declaration from the interface
change Checkbox so hovered is a public property
it might be possible to change the accessor dynamically using the as3 commons bytecode project (http://www.as3commons.org/as3-commons-bytecode/emit.html), but I'm not 100% sure.

How to cast a Proxy to an interface? (or tell Proxy to implement an interface)

I need ActionScript Proxy to be castable to a particular interface.
Here is an example without interface:
public dynamic class Tracer extends Proxy {
flash_proxy override function callProperty(method:*, ... args):* {
trace(method + " " + args)
}
}
var t:* = new Tracer()
t.sayHello("123") // prints: "sayHello [123]"
Now I need "t" to be of Talker type (don't ask why, I just love static typing):
public interface Talker {
function sayHello(s:String):void
}
var t:Talker = new Tracer() // throws class cast exception
t.sayHello("123")
The question is: how to cast a proxy?
For example, a solution for Java would be passing a list of interfaces when you create a new Proxy http://download.oracle.com/javase/6/docs/api/java/lang/reflect/Proxy.html
Is it really possible with ActionScript 3?
That's unfortunately not possible in plain actionscript. But I think you can do it with the as commons bytecode API.
what about declaring public dynamic class Tracer extends Proxy implements Talker with all methods that need to be defined?

Mixin or Trait implementation in AS3?

I'm looking for ideas on how to implement a Mixin/Trait style system in AS3.
I want to be able to compose a number of classes together into a single object. Of course this is not a language level feature of AS3, but I'm hoping that there is maybe some way to do this using prototype based techniques or maybe some bytecode hacking that I believe AsMock uses to implement it's functionality.
An existing Java example is Qi4J where the user define interfaces that the Qi4j framework implements based on metadata tags and coding by convention.
Has anyone any ideas on how to get the Mixin/Trait concept working within AS3?
Zero solutions presented on this, so I looked into a few methods. There are ECMA script style mixins by adding methods defined on other objects to the base objects prototype. But this means that the advantages of static typing are gone.
I was looking for a solution that didn't sidestep the static type system. I knew that ASMock used bytecode injection to create proxy classes. I hacked around ASMock for the past few days and came up with a possible solution implemented by creating a class with composed classes (through bytecode injection).
From the users point of view this involves defining your object that uses mixins through many interfaces:
public interface Person extends RoomObject, Moveable
public interface RoomObject
{
function joinRoom(room:Room):void
function get room():Room
}
public interface Moveable
{
function moveTo(location:Point):void
function get location():Point
}
Then you define classes to represent these interfaces:
public class MoveableImpl implements Moveable
{
private var _location:Point = new Point()
public function get location():Point { return _location }
public function move(location:Point):void
{
_location = location.clone()
}
}
public class RoomObjectImpl implements RoomObject
{
private var _room:Room
public function get room():Room { return _room }
public function joinRoom(room:Room):void
{
_room = room
}
}
In a normal situation where you want to compose classes you would write:
public class PersonImpl implements Person
{
private var _roomObject:RoomObject = new RoomObjectImpl()
private var _moveable:Moveable = new MoveableImpl()
public function get room():Room { return _roomObject.room }
public function joinRoom(room:Room):void { _roomObject.joinRoom(room) }
public function get location():Point { return _moveable.location }
public function move(location:Point):void { _moveable.move(location) }
}
This is easily written using code due to it's regular layout. And that is exactly what my solution does, by injecting the equivilant bytecode into a new class. With this bytecode injection system we can create a Person object like so:
public class Main
{
private var mixinRepo:MixinRepository = new MixinRepository()
public function Main()
{
with(mixinRepo)
{
defineMixin(RoomObject, RoomObjectImpl) // associate interfaces with concreate classes
defineMixin(Moveable, MoveableImpl)
defineBase(Person)
prepare().completed.add(testMixins) // the injection is a async process, just liek in ASMock
}
}
private function testMixins():void
{
var person:Person = mixinRepo.create(Person)
var room:Room = new Room('room you can play in')
person.joinRoom(room)
trace('person.room:', person.room)
person.move(new Point(1, 2))
trace('person.location:', person.location)
}
}
At the moment this system is a proof of concept and is therefore very basic and brittle. But it shows that it is possible to come close to a Scala mixin/traits style system to AS3. I've made a github project to hold the code if anyone is interested in running the solution and poking around at how it was done.
A more complete example is given on the project wiki.
Look here, this works, mixes in methods and is simple.
http://github.com/specialunderwear/as3-mixin
o, and it works when you compile in as3 mode.
I found this one in Realaxy -- http://realaxy.com/