Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

Constructor-Factory

Here is a way to use the constructor of an abstract-class
as factory:




Object.prototype.addClass = function (pConstructor) {
   // Adds a class to an object, to the
   // beginning of the proto-chain.
   var tmp = this.__proto__;
   this.__proto__ = pConstructor.prototype;
   this.__proto__.__proto__ = tmp;
}
ASSetPropFlags(Object.prototype, null, 1, 0);

// Abstract-Factory-Constructor
AbstractCarClass = function (pConcreteClass) {
   // default-properties
   this.intMaxSpeed = 130;
   this.intPrice = 15000;
   //
   this.addClass(pConcreteClass);
   this.init();
}
AbstractCarClass.prototype.changeClass = function (pConcreteClass, pBlnInitialize) {
   this.__proto__ = pConcreteClass.prototype;
   if (pBlnInitialize) this.init();
}
AbstractCarClass.prototype.init = function () {
   //
   // Should be overwritten in concrete-class to
   // initialize properties.
   //
}
// concrete-class
PorscheClass = function () {}
PorscheClass.prototype.init = function () {
   this.intMaxSpeed = 240;
   this.intPrice = 70000;
}
PorscheClass.prototype.toString = function () {
   return '[object PorscheClass]';
}
// concrete-class
LanciaClass = function () {}
PorscheClass.prototype.init = function () {
   this.intMaxSpeed = 120;
   this.intPrice = 10000;
}
LanciaClass.prototype.toString = function () {
   return '[object LanciaClass]';
}
// test
objCar = new AbstractCarClass(PorscheClass);
trace(objCar); // [object PorscheClass]
//
objCar.changeClass(LanciaClass, true);
trace(objCar); // [object LanciaClass]

Posted by hOk at April 27, 2003 04:49 PM

Comments (2)

Hello!,
I've discover this blog through the Quasimondo's blog...and I must say that I've add this link quickly to my favourites!
there's a lot of quality entries here.
thanks.
C.

Posted by Carlos at April 28, 2003 01:27 PM

Thanks,
please note that the in addClass-Method is
revised, you find it here
...the old one cant deal with multiple
inheritance, the proto-chain will be cutted
off.
regards, Holger(hOk)

Posted by hOk at April 30, 2003 05:04 PM


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place