开发者

AS3 internal and custom namespaces

I have the following packages:

spark

    spark.engine

Within spark I have a class SeCore; and within spark.engine I have SeStepper and SeKeyboard.

What I'm trying to achieve is have SeCore as being the only class that can create an instance of SeStepper or SeKeyboard. This can be achieved by moving SeCore into the spark.engine package and making the other two classes internal, but I'd like to have SeCore in the spark package if possible.

I've tried making my own namespace to handle this, like so:

package spark.engine
{
    import spark.namespaces.spark_core;

    use namespace spark_core;

    spark_core class SeStepper extends SeObject
    {
        //
    }
}

However I get the error:

1116: A us开发者_JAVA技巧er-defined namespace attribute can only be used at the top level of a class definition.

Are there any other approaches I can take to achieve what I'm after?


99% of the time, marking anything as 'internal' is a bad idea. It's better to have a naming convention for 'off-limits' classes and members, and allow developers to go there at their own risk. Marking things as 'internal' or 'private' is something that should only be done rarely, and with great forethought.

However, you could enforce this behavior at run time by using a read-only property in SeCore and checking its value from SeStepper and SeKeyboard.

Following is pseudocode, haven't used AS3 in a while.

In SeCore

private var _createAuthorized = false;
public function get CreateAuthorized():boolean {return _createAuthorized;}

private function createSeStepper(){
  _createAuthorized = true;
  var obj = new SeStepper(this)
  _createAuthorized = false;
  return obj;
}

in SeStepper

public function SeStepper(core:SeCore){
  if (!core.CreateAuthorized) throw new Error("Only SeCore can do this");
}


I can't agree with the answer, i mean making things public is way to invite hackers. I can execute any public functions in any flash running on my computer in any context i want, i can even override their execution in memory since they are easy to find, whereas doing something like that with private/internal functions is almost impossible.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜