dzhedzho
June 8th, 2008, 06:29 AM
Hello guys, if some of you have been wondering how to create abstract classes in AS3, here is an idea. The following class Abstract cannot be instantiated, however its subclasses can. The Abstract class does not have to be in a package, I just placed itin one so you note the name
package hd{
import flash.utils.getQualifiedClassName;
public class Abstract {
public function Abstract () {
if (getQualifiedClassName(this)=="hd::Abstract") {
throw new ArgumentError("Error 2012: class Abstract cannot be instantiated.");
}
}
}
}
Seems that the common way to do this seems to be
package{
public class CommonSingleton {
private static var instance:CommonSingleton;
public function CommonSingleton (enforcer:SingletonEnforcer) {
}
public static function getInstance ():CommonSingleton {
if (instance==null) {
instance = new CommonSingleton(new SingletonEnforcer);
}
return (instance);
}
}
}
class SingletonEnforcer {
}
However the error message thrown when trying to create object from this class is rather (un)informative
//1136: Incorrect number of arguments. Expected 1.
In addition you can still create an instance using the constructor passing null as parameter. Something which you quite possibly will try while scratching your head over the error above
Well, here is another way to do this
package{
public class Singleton{
private static var instance:Singleton;
private static var direct:Boolean = true;
public function Singleton () {
if (direct) {
throw new Error ("Use Singleton.getInstance() instead");
}
}
public static function getInstance ():Singleton {
if (instance!=null) {
return (instance);
}else{
direct = false;
instance = new Singleton();
direct = true;
return (instance);
}
}
}
}
Cheers
package hd{
import flash.utils.getQualifiedClassName;
public class Abstract {
public function Abstract () {
if (getQualifiedClassName(this)=="hd::Abstract") {
throw new ArgumentError("Error 2012: class Abstract cannot be instantiated.");
}
}
}
}
Seems that the common way to do this seems to be
package{
public class CommonSingleton {
private static var instance:CommonSingleton;
public function CommonSingleton (enforcer:SingletonEnforcer) {
}
public static function getInstance ():CommonSingleton {
if (instance==null) {
instance = new CommonSingleton(new SingletonEnforcer);
}
return (instance);
}
}
}
class SingletonEnforcer {
}
However the error message thrown when trying to create object from this class is rather (un)informative
//1136: Incorrect number of arguments. Expected 1.
In addition you can still create an instance using the constructor passing null as parameter. Something which you quite possibly will try while scratching your head over the error above
Well, here is another way to do this
package{
public class Singleton{
private static var instance:Singleton;
private static var direct:Boolean = true;
public function Singleton () {
if (direct) {
throw new Error ("Use Singleton.getInstance() instead");
}
}
public static function getInstance ():Singleton {
if (instance!=null) {
return (instance);
}else{
direct = false;
instance = new Singleton();
direct = true;
return (instance);
}
}
}
}
Cheers