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