Implementing Mixin or Trait in AS3?

I am looking for ideas on how to implement a Mixin / Trait style system in AS3.

I want to be able to compose several classes together in one object. Of course, this is not a function of the AS3 language level, but I hope that there may be a way to do this using prototypes based on methods or, perhaps, some hack codes, which, in my opinion, use AsMock to implement its functionality.

An existing Java Qi4J example where the user defines the interfaces that the Qi4j framework implements based on metadata tags and convention coding.

Are there any ideas on how to use the Mixin / Trait concept in AS3?

+5
source share
3 answers

This presents zero solutions, so I looked at a few methods. There is an ECMA script style mixins , adding methods defined on other objects to the prototype base objects. But this means that the benefits of static input are missing.

I was looking for a solution that did not interfere with the static type system. I knew that ASMock is used to embed proxy classes. Over the past few days, I hacked ASMock and came up with a possible solution created by creating a class with compiled classes (via byte injection).

From the point of view of users, this is related to the definition of 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 
}

:

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
    }
}

, , :

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) }
}

- . , , - . - Person :

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)
    }
}

. , AS3 Scala mixin/traits style. github , , - , .

wiki.

+7

I found this on Realaxy - http://realaxy.com/

+1
source

All Articles