Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-18-2007, 09:34 PM   #1
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default Decorator Pattern

I've got a question about the Decorator Pattern. Is the purpose of the Decorator Pattern to extend functionality of an already given method of an component? Or can/does it allow additional methods to be added to certain instances of a component?

For example, all of the examples I've seen so far (and I can't read C++ very well....) simply extend append functionality to a method of an instance. E.g.
ActionScript Code:
package {    public class Component    {       protected var _foo:String;       public function foo():String { return _foo; }    } } // // package {    public class ConcreteComponent    {       public function ConcreteComponent()       {           _foo = "hello world";       }    } } // // package {    public class ConcreteDecorator extends Component    {       protected var _component:Component;       public function ConcreteDecorator(cmpt:Component)       {          _component = cmpt;       }       override public function foo():String       {          return _component.foo() + " fudcake";       }    } }

However, can and do concrete decorators add additional functionality or does this call for a different design pattern.
ActionScript Code:
package {    public class ConcreteDecorator extends Component    {       protected var _component:Component;       public function ConcreteDecorator(cmpt:Component)       {          _component = cmpt;       }       override public function foo():String       {          return _component.foo() + " fudcake";       }       /**        * Bad example but shows my point.        *<p>Is decorator used to add additional methods?</p>        */       public function addGlow():void       {          _component.filters = [new GlowFilter()];       }    } }

My guess is decorator is used only to extend functionality of a method already in the component.


1 More Question:
What is the point of the abstract decorator class? In my example above you can see I left it out. But with something like this from here
ActionScript Code:
/**  * C++ code NOT actionscript  */ abstract class Decorator : Component   {     protected Component component;     public void SetComponent(Component component)     {       this.component = component;     }     public override void Operation()     {       if (component != null)       {         component.Operation();       }     }   }
you can see that the override method doesn't do anything. It returns of the original method, so what is the point?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman

Last edited by Flash Gordon; 08-18-2007 at 09:49 PM..
Flash Gordon is offline   Reply With Quote
Old 08-20-2007, 08:56 AM   #2
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,859
Default

decorater does both the things you asked originally.
then that c++ class is an abstract class so you cannot use it HAS to be extended.
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 08-20-2007, 08:58 AM   #3
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

cool. thanks for the reply. I got that book you mentioned and it is soooo much better than the AS 3 design patterns.

__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-20-2007, 10:05 AM   #4
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,859
Default

i have a perma copy on my desk
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 08-20-2007, 07:58 PM   #5
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Yea, it's good stuff!

However, I've read both books and some online stuff, and I'm just not clear what purpose the Abstractor Decortator server. When even have it? Why not directly subclass from the Abstract Component?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-20-2007, 10:13 PM   #6
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

http://www.schelterstudios.com/blog/?p=28
http://www.schelterstudios.com/blog/?p=29

Two blog entries I wrote about decorators in AS 3

The abstract decorator should have additional logic that you need to create decorators that the abstract component alone wouldn't have, such as the ability to aggregate other instances into itself.

Last edited by Assertnfailure; 08-20-2007 at 10:16 PM..
Assertnfailure is offline   Reply With Quote
Old 08-27-2007, 09:31 PM   #7
drexle
...
 
Join Date: Mar 2005
Posts: 248
Default

What book is it that you're using? I'd really like to learn more about patterns and tested development solutions and practices.
drexle is offline   Reply With Quote
Old 08-27-2007, 11:34 PM   #8
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Head First Design Patterns By O'Reilly. It is a Java book
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 08-28-2007, 02:34 AM   #9
hangalot
lala
 
hangalot's Avatar
 
Join Date: Feb 2002
Location: on the road
Posts: 2,859
Default

are you slaggin my fav DP book? i will take offence :s
__________________
oi poloi
http://www.memorphic.com/news/
hangalot is offline   Reply With Quote
Old 08-28-2007, 04:42 AM   #10
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Nah, that book is great! I read about 1/2 the book in a week. One of the best books I've read. Thanks for the heads up about it.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:20 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.