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)
defineMixin(Moveable, MoveableImpl)
defineBase(Person)
prepare().completed.add(testMixins)
}
}
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.