PDA

View Full Version : Decorator Pattern


Flash Gordon
08-18-2007, 08:34 PM
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.

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.

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 (http://www.dofactory.com/Patterns/PatternDecorator.aspx#_self1)

/**
* 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?

hangalot
08-20-2007, 07:56 AM
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.

Flash Gordon
08-20-2007, 07:58 AM
cool. thanks for the reply. I got that book you mentioned and it is soooo much better than the AS 3 design patterns.

:)

hangalot
08-20-2007, 09:05 AM
:) i have a perma copy on my desk :)

Flash Gordon
08-20-2007, 06:58 PM
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?

Assertnfailure
08-20-2007, 09:13 PM
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.

drexle
08-27-2007, 08:31 PM
What book is it that you're using? I'd really like to learn more about patterns and tested development solutions and practices.

Flash Gordon
08-27-2007, 10:34 PM
Head First Design Patterns By O'Reilly. It is a Java book

hangalot
08-28-2007, 01:34 AM
are you slaggin my fav DP book? i will take offence :s

Flash Gordon
08-28-2007, 03:42 AM
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.

hangalot
08-28-2007, 03:45 AM
not me, everyone has been telling you about it. remember that some patterns solve issues we never encouter when we write UI's