开发者

Library design quandary

开发者 https://www.devze.com 2022-12-17 08:05 出处:网络
Ok so I am writing an open source library. A section of this library deals with moving an entity in a two and three dimensional space so it will have functions that manipulate the rotation, position e

Ok so I am writing an open source library. A section of this library deals with moving an entity in a two and three dimensional space so it will have functions that manipulate the rotation, position etc.

Now ideally I would like my library to work well with other libraries, in particular things like Papervision3D and other Flash 3D engines, but not forgotting basic image objects like a Sprite or Movieclip.

So this is my quandary. The functions that will manipulate the entity will also need to modify the values of the underlying model data (so either a Sprite, Pap开发者_如何学Goervision object etc). What is the best way to make my library flexible so that it can support multiple data models. Performance is also important aspect too.

Currently I am thinking of something like this:

  //this is the public function that I expose in my library
  public function rotate(val:Number,func:Function,objData:*):void
  {
 func(val,objData);
  }

  //example of a function that could be passed in
  //this one will rotate a MovieClip
  private function modelFunction1(rot:Number,objData:*):void
  {
 var myMov:MovieClip  = objData as MovieClip;
 myMov.rotation = rot;
  }

  //second example of a function that could be pass in
  //this one will rotate a point
  private function modelFunction2(rot:Number,objData:*):void
  {
     //yes I know this piece of code makes no sense :P
     var p:Point = objData as Point;
  p.x = Math.cos(rot);
     p.y = Math.sin(rot);
  }

so then it could be used like:

rotate(4,modelFunction2,myPoint)

//or

rotate(4,modelFunction1,mySprite);

I should add that in reality I, as the client code, won't be able to directly call the rotate function. Instead the rotate function that I want to pass in would need to be stored somewhere as a class member and then be called by the rotate function. Its just less code for me to write it out like above.

This to me seems quite flexible although the performance implications of casting and passing functions concerns me (but might be ok). Can anyone else suggest an alternative or is what I have the most logical solution. Thanks :)


I suggest the adapter pattern.

In your case you could define interfaces which offer type safe definitions for what your library expects instead of having function arguments. then you need to write adapter classes which implement your librarys interfaces and wrap for instance a papervision object and delegate the function calls to your interface methods to the papervision object.

interface IRotatatable {
   function rotate(deg : Number) : void
}

class YourLibraryClass {

    public function rotate(r : IRotatatable, val : Number):void {
       r.rotate(val)
    } 
}


class P3DAdapter implements IRotatable {
    public function P3DAdapter(p3d : SomePaperVisionObject) {
       _p3d  = p3d;
    }

    public function rotate(r :Number):void {
       p3d.rot = r;
    }
}


function someClientCode():void {
    var adapter : IRotatable = new P3DAdapter(p3d)
    new SomeLibraryClass().rotate(adapter, val));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消