在AS3中混入或特质的实施? [英] Mixin or Trait implementation in AS3?

查看:120
本文介绍了在AS3中混入或特质的实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找关于如何实现的混入/特质风格系统在AS3。

我希望能够组成一个几个类组合成一个单一的对象。当然,这不是AS3的语言水平的功能,但我希望,有可能一些方法采用基于原型的技术,或者一些字节code黑客,我相信AsMock使用来实现它的功能。<做到这一点/ P>

这是现有的Java例子是 Qi4J 在用户定义接口的Qi4j框架实现基于元数据标签,并按照约定的编码。

有没有人对如何获得混入/特质概念中AS3工作任何想法?

解决方案

psented这个零解$ P $,所以我看着几个方法。有<一href="http://peter.michaux.ca/articles/transitioning-from-java-classes-to-javascript-prototypes">ECMA剧本风格混入通过添加其他对象的基本对象原型中定义的方法的。但是,这意味着静态类型的优势都没有了。

我一直在寻找未回避静态类型系统的解决方案。我知道, ASMock 使用字节code注射液,创建代理类。我砍死围绕 ASMock ,在过去的几天,想出了通过由类创建一个类(通过字节$ C实现了一个可能的解决方案$ C注射液)。

从用户的角度来看,这涉及定义你的对象使用混入通过多种接口:

 公共接口人扩展RoomObject,可移动

公共接口RoomObject
{
    功能joinRoom(房:房间):无效
    函数来获取室():客房
}

公共接口可移动
{
    功能MOVETO(地点:点):无效
    函数来获取位置():点
}
 

然后定义类重新present这些接口:

 公共类MoveableImpl实现可移动
{
    私人VAR _location:点=新的点()
    公共函数来获取位置():点{返回_location}

    公共职能的举动(地点:点):无效
    {
        _location = location.clone()
    }
}

公共类RoomObjectImpl实现RoomObject
{
    私人VAR _room:房间
    公共职能的get室():客房{返回_room}

    公共职能joinRoom(房:房间):无效
    {
        _room =房间
    }
}
 

在您要编写课程,你会写一个正常的情况:

 公共类PersonImpl的实现了人
{
    私人VAR _roomObject:RoomObject =新RoomObjectImpl()

    私人VAR _moveable:可移动=新MoveableImpl()

    公共职能的get室():客房{返回_roomObject.room}

    公共职能joinRoom(房:房间):无效{_roomObject.joinRoom(室)}

    公共函数来获取位置():点{返回_moveable.location}

    公共职能的举动(地点:点):无效{_moveable.move(位置)}
}
 

使用code,由于它的常规布局这是很容易写的。而这也正是我的解决方案呢,通过注入同等学历字节code到一个新的类。有了这个字节code注射液系统,我们可以创建一个Person对象,像这样:

 公共类主
{
    私人VAR mixinRepo:MixinRepository =新MixinRepository()

    公共函数main()
    {
        与(mixinRepo)
        {
            defineMixin(RoomObject,RoomObjectImpl)//与concreate类准接口
            defineMixin(移动,MoveableImpl)
            defineBase(人)
            prepare()。completed.add(testMixins)//注射是一个异步的过程,只是liek在ASMock
        }
    }

    私有函数testMixins():无效
    {
        变种人:人= mixinRepo.create(人)
        VAR房间:房间=新房间('房间,您可以起到)

        person.joinRoom(室)
        跟踪('person.room:,person.room)

        person.move(新点(1,2))
        跟踪('person.location:,person.location)
    }
}
 

目前,该系统是一个概念验证,因此是很基本的,脆。但它表明,有可能接近Scala的混入/特质的风格体系,AS3。我做了一个github上项目持有code。如果有人有兴趣运行的解决方案,并在关注着它是怎么做。

一个更完整的例子给出该项目的维基

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.

这篇关于在AS3中混入或特质的实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆