Why there are no private constructors in AS3 versi

2019-05-05 17:42发布

I am confused: In AS3, why do we keep the Singleton class constructor public and not private, like in Java? If we keep the constructor public, then we can directly access it from the outside!

Please check the MODEL part in this example.

4条回答
We Are One
2楼-- · 2019-05-05 17:46

Actionscript 3 does not support private constructors.

In order to enforce the singleton pattern, many developers cause the constructor to raise an exception if there is already a singleton instance created. This will cause a runtime error, instead of a compile time error, but it does prevent the singleton's inappropriate use.


Example:

public static var instance:MySingleton;

public MySingleton(){
    if (instance != null) {
        throw new Error("MySingleton is a singleton. Use MySingleton.instance");
    }else {
        instance = this;
    }
}
查看更多
Melony?
3楼-- · 2019-05-05 17:56

This blog post, in which Sho Kuwamoto (formerly of Macromedia / Adobe and heavily involved in the development of the Flex platform and tooling) explains the decision to omit private and protected constructors from ActionScript 3.0, may be of tangential interest.

Macromedia / Adobe's involvement in the developing ECMAScript 4 standard, and the decision to make ActionScript 3.0 adhere as closely to the specification as possible, meant that they were faced with the choice of either bodging these features, omitting them from the language completely, or waiting until they were standardised (and delaying the release of the language as a result).

Interesting, I think, because it shines a light on some of the key issues in the open versus proprietary standards debate.

查看更多
萌系小妹纸
4楼-- · 2019-05-05 18:01

Here is an implementation of a singleton in AS with internal class usage:

package{
    public class Singleton{
        private static var _instance:Singleton=null;
        public function Singleton(e:SingletonEnforcer){
            trace("new instance of singleton created");
        }
        public static function getInstance():Singleton{
            if(_instance==null){
                _instance=new Singleton(new SingletonEnforcer());
            }
            return _instance;
        }
    }
} 
//I’m outside the package so I can only be accessed internally
class SingletonEnforcer{
//nothing else required here
}

See details here: http://www.swinburne.edu.au/design/tutorials/P-flash/T-How-to-build-a-Singleton-in-Actionscript-3/ID-143/

查看更多
小情绪 Triste *
5楼-- · 2019-05-05 18:04

With the flex framework comes a class "Singleton" that allows you to
register a class to an interface.
With this method you can hide any functions you want just by not including it in the interface

// USAGE: is as simple as importing the class and then calling the
// method you want.
import com.samples.Sample

// and then simple just doing
trace( Sample.someFunction( ) ) // will return true

// Sample.as

package com.samples{
  import flash.utils.getDefinitionByName;
  import mx.core.Singleton;

  public class Sample{
    //private static var implClassDependency:SampleImpl;
    private static var _impl:ISample;

    // static methods will call this to return the one instance registered 
    private static function get impl():ISample{
      if (!_impl)   {
        trace( 'registering Singleton Sample' )
        Singleton.registerClass("com.samples::ISample",com.samples.SampleImpl);
        _impl = ISample( Singleton.getInstance("com.samples::ISample"));
      }
      return _impl;
    }

    public static function someFunction( ):Boolean {
      return impl.someFunction(   )
    }
  }
}

// ISample.as

package com.samples{
  public interface ISample {
    function someFunction( ):Boolean;
  }
}

// SampleImpl.as

package com.samples{

  // ExcludeClass will hide functions from the IDE
  [ExcludeClass]
  // we can extends a class here if we need 
  public class SampleImpl implements ISample{
    // the docs say we need to include this but I donno about that
    // include "../core/Version.as";

    public function SampleImpl (){
      // call super if we are extending a class
      // super();
    }

    // instance will be called automatically because we are
    // registered as a singleton
    private static var instance:ISample;
    public static function getInstance():ISample{
      if (!instance)
        instance = new SampleImpl()
        return instance;
      }
    }


    // public functions where we do all our code
    public function someFunction( ):Boolean {
      return true;
    }
}
查看更多
登录 后发表回答