Mmmm I really want a bagel now!!! Damn that decor....

On 1/30/07, JOR <[EMAIL PROTECTED]> wrote:

A decorator object composites the object it wishes to decorate and is
used in it's place.  Since the decorator inherits from the the same base
class as the object it decorates they can both be used interchangeably
through polymorphism.

consider something like this:

var myBagel:Bagel = new Bagel();
trace (myBagel.getCalories()); // 200

// Add creamcheese to my bagel
myBagel = new CreamCheeseDecorator(myBagel);
trace (myBagel.getCalories()); // 300

// Add lox to my bagel
myBagel = new LoxDecorator(myBagel);
trace (myBagel.getCalories()); // 330

//---------------
// bagel looks something like this
public class Bagel {
   public function getCalories ():uint {
     return 200;
   }
}
//inside the decorator is something like this:
public class CreamCheeseDecorator extends Bagel {
   private var _bagel:Bagel;
   public function CreamCheeseDecorator (bagel:Bagel) {
     _bagel = bagel;
   }
   public function getCalories ():uint {
     return _bagel.getCalories() + 100;
   }
}
//inside the decorator is something like this:
public class LoxDecorator extends Bagel {
   private var _bagel:Bagel;
   public function LoxDecorator (bagel:Bagel) {
     _bagel = bagel;
   }
   public function getCalories ():uint {
     return _bagel.getCalories() + 30;
   }
}

You can add more Bagel types like EggBagel and EverythingBagel and more
Decorator objects like Butter and use them all interchangeably.

note, this untested code, I just typed it out in the post so their might
be typos.


James O'Reilly  —  Consultant
Adobe Certified Flash Expert
http://www.jamesor.com
Design • Code • Train



Erik Bianchi wrote:
> Opps meant to add: that's my interpretation anyways. I could be wrong.
I'm
> wrong all the time. In fact I consider myself a professional mistake
maker.
>
> -erik
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Erik
Bianchi
> Sent: Tuesday, January 30, 2007 1:49 PM
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Flair Pattern bad mixins good (?)
>
> A decorator is meant to be dynamic. It adds responsibilities to an
object at
> run time.
>
> You take ComponentA and add methods to it on mouseclick, that's a
decorator.
>
> ComponentB is made up of ComponentC and ClassA, that's a composite.
>
> My implementation of a mixin which I borrowed from java is really just
using
> composites + interfaces to emulate multiple inheritance.
>
> Decorators and composites are similar however in that they are both
> structural patterns and define an interface for communicating between
parent
> and "child" components / classes.
>
> Best,
>
> -erik
>
>
>
>
>
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of T.
Michael
> Keesey
> Sent: Tuesday, January 30, 2007 9:00 AM
> To: Flashcoders mailing list
> Subject: Re: [Flashcoders] Flair Pattern bad mixins good (?)
>
> How is this any different from a Decorator/Wrapper? Looks like a
> double decorator.
>
> On 1/30/07, Erik Bianchi <[EMAIL PROTECTED]> wrote:
>
>>Actually my definition of a mixin is very strict compared to a
decorator;
>
> it
>
>>uses design by contract, composition and declares type:
>>
>>Class ClassA implements IClassB, IClassC
>>{
>>
>>private var classB:ClassB;
>>private var classC:ClassC;
>>
>>private function classBMethod():Boolean{...};
>>
>>private function classCMethod():Number{...};
>>
>>}
>>
>>
>>-erik
>>
>>
>>-----Original Message-----
>>From: [EMAIL PROTECTED]
>>[mailto:[EMAIL PROTECTED] On Behalf Of T.
Michael
>>Keesey
>>Sent: Tuesday, January 30, 2007 12:09 AM
>>To: Flashcoders mailing list
>>Subject: Re: [Flashcoders] Flair Pattern bad mixins good (?)
>>
>>On 1/29/07, David Ham <[EMAIL PROTECTED]> wrote:
>>
>>>startObjectDrag triggered by     obj_mc.onPress
>>>checkForSnap    triggered by    setInterval or onEnterFrame type of
>
> event,
>
>>>in this case onObjectDrag
>>>stopObjectDrag  triggered by    obj_mc.onRelease
>>
>>This looks more like the Broadcaster pattern or the Event Dispatcher
>>(a.k.a. Observer) pattern than Decorator.
>>
>>(Also, it might be better to tie checkForSnap to mouseMove.)
>>
>>Personally, I'm not a big fan of mix-ins because, well, they're kind
>>of sloppy. They involve tinkering with stuff that should be off-limits
>>(and is in AS3, I think). Using mix-ins, you could accidentally use a
>>non-function as a function. That can't happen if you stick to
>>strictly-typed programming.
>>--
>>T. Michael Keesey
>>_______________________________________________
>>Flashcoders@chattyfig.figleaf.com
>>To change your subscription options or search the archive:
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>Brought to you by Fig Leaf Software
>>Premier Authorized Adobe Consulting and Training
>>http://www.figleaf.com
>>http://training.figleaf.com
>>
>>
>>_______________________________________________
>>Flashcoders@chattyfig.figleaf.com
>>To change your subscription options or search the archive:
>>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>Brought to you by Fig Leaf Software
>>Premier Authorized Adobe Consulting and Training
>>http://www.figleaf.com
>>http://training.figleaf.com
>>
>
>
>
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com




--
[EMAIL PROTECTED]
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to