macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript
Brandon Hall and Samual Wan developped the
Flair-Pattern. It is a realy good technik
to add beaviours to allready existing objects.
The Flair-Object is simply copied into the
object which we would extend.
The Flair Pattern is similary to the state-pattern
because our subject aggregates the flair-object.
This is quit usefull because we can change, remove
and add functionality add runtime. This look of
our object can completly change, like as our object
changed the class.
Hence, let us look to my interpretation of the flair-
patter with example code.
//
// > Now we create our first flair-class
//
// First we setup a namespace for our flair-classes
if (CarFlair == undefined) {
_global.CarFlair = new Object();
}
// ===============================
// Now setup our first flair-class
// ===============================
CarFlair.SoundFlairFuelEmpty = function () {}
// =============================================================
// Setting up the standard-methods that belongs to flair-classes
// =============================================================
// This class-method is needed to add the flair to a car
CarFlair.SoundFlairFuelEmpty.addFlair = function (pObjCar) {
//
// Now let us copy the Flair into the carObject. We
// modify the car-object, because we add a property.
pObjCar.$objSoundFlair = new this();
//
// Our flair needs a reference to the car-Object
pObjCar.$objSoundFlair.objCar = pObjCar;
//
// Well, now our soundflair-object need to know if the,
// if the fuel status changed, so we let it listen to
// the cars broadcaster
pObjCar.addListener(pObjCar.$objSoundFlair);
}
//
// Method to remove a the flair
CarFlair.SoundFlairFuelEmpty.removeFlair = function (pObjCar) {
pObjCar.removeListener(pObjCar.$objSoundFlair);
//
delete pObjCar.$objSoundFlair;
}
// ========================================
// Setup the specific methods of this flair
// ========================================
CarFlair.SoundFlairFuelEmpty.prototype.onFuelFilled = function () {
//
// our tank was filled, so we can change the flair
CarFlair.SoundFlairFuelEmpty.removeFlair(this.objCar);
CarFlair.SoundFlairFuelFilled.addFlair(this.objCar);
}
//
CarFlair.SoundFlairFuelEmpty.prototype.onEngineStart = function () {
trace('put....pt...p....');
}
//
// > Ok, know lets create another flair that should be used
//
//
// class
CarFlair.SoundFlairFuelFilled = function () {}
//
// flair-class-methods
CarFlair.SoundFlairFuelFilled.addFlair = function (pObjCar) {
pObjCar.$objSoundFlair = new this();
pObjCar.$objSoundFlair.objCar = pObjCar;
pObjCar.addListener(pObjCar.$objSoundFlair);
}
//
CarFlair.SoundFlairFuelFilled.removeFlair = function (pObjCar) {
pObjCar.removeListener(pObjCar.$objSoundFlair);
delete pObjCar.$objSoundFlair;
}
//
// flair-object-methods
CarFlair.SoundFlairFuelFilled.prototype.onFuelEmpty = function () {
CarFlair.SoundFlairFuelFilled.removeFlair(this.objCar);
CarFlair.SoundFlairFuelEmpty.addFlair(this.objCar);
}
//
CarFlair.SoundFlairFuelFilled.prototype.onEngineStart = function () {
trace('bruuummm, broouuhhmm, brruuemmm');
}
//
// > And know create the car-class
//
//
// class
Car = function () {
// we need our car to be an Broadcaster for the
// events: onEngineStart, onFuelEmpty, onFuelFilled
ASBroadcaster.initialize(this);
//
// ok let us assume that our tank is filled
CarFlair.SoundFlairFuelFilled.addFlair(this);
//
//ASSetPropFlags(this,null,0,1);
}
//
// methods
Car.prototype.startEngine = function () {
this.broadcastMessage('onEngineStart');
}
//
Car.prototype.onFuelFilled = function () {
this.broadcastMessage('onFuelFilled');
}
//
Car.prototype.onFuelEmpty = function () {
this.broadcastMessage('onFuelEmpty');
}
//
// > Now testing the complete
//
objCar = new Car();
objCar.startEngine(); // bruuummm, broouuhhmm, brruuemmm
objCar.onFuelEmpty();
objCar.startEngine(); // put....pt...p....
objCar.onFuelFilled();
objCar.startEngine(); // bruuummm, broouuhhmm, brruuemmm
The Flair has did the change by them self, sometimes
it is better to let your object do the change.
The coupling with events is mostly a flexible way, but
it is also possible to call direct the flair-methods,
then the object must know thair flair-objects and we have
nearly exactly the state-patter.
I like the flexibility of flair, also it is very
good to use for extending Textfield-objects, like Brandon
and Wan did it in there book.
Posted by hOk at May 11, 2003 01:38 PM
Comments (3)
Hi Holger,
i wonder if you could add an additional level (eg. like CarFlair.SoundFlair.FuelEmpty) and put the addFlair and removeFlair-method into this new level instead of putting them into every concrete SoundFlair class. The concrete flair could be a parameter to addFlair.
Posted by bokel at May 11, 2003 08:49 PM
Hi, yes that is an interesting approach,
but mostly the flairs have different ways,
to snap to the target-object, so you will
need an init-method in your flair-class.
Maybe i did not understand what you mean,
have you an example?
regards, Holger
Posted by hOk at May 12, 2003 06:15 PM
Sorry, i ment something like that
CarFlair.SoundFlair.addFlair = function (pObjCar, pStrFlairName) {
pObjCar.$objSoundFlair = new this[pStrFlairName]();
//...
CarFlair.SoundFlair.FuelFilled = function(){}
CarFlair.SoundFlair.FuelEmpty = function(){}
//...
CarFlair.SoundFlair.addFlair( this, "FuelFilled");
This way all of the SoundFlairs could share the addFlair and removeFlair-methods of SoundFlair.
Posted by bokel at May 12, 2003 08:23 PM
|
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. |
