|
|
|
|
|
|
s*****w 发帖数: 1527 | 1 the original standard sample is like this,
abstract class Component
{
public abstract void Operation();
}
class ConcreteComponent : Component
{
public override void Operation()
{
Console.WriteLine("ConcreteComponent.Operation()");
}
}
abstract class Decorator : Component
{
protected Component component;
....
} | b*****e 发帖数: 7 | 2 The Decorator constructor only needs to take a reference/pointer to
Component as parameter. It doesn't need to construct the Component. The
Component should have been already constructed when it is passed to
Decorator. | s*****w 发帖数: 1527 | 3 thanks for reply 1st,
my q is from this,
http://www.dofactory.com/Patterns/PatternDecorator.aspx#_self1
if class Component has a constructor taking 10 parameters,
how to make it work ?
as Decorator is asking for paramters now.
【在 b*****e 的大作中提到】 : The Decorator constructor only needs to take a reference/pointer to : Component as parameter. It doesn't need to construct the Component. The : Component should have been already constructed when it is passed to : Decorator.
| s*****w 发帖数: 1527 | 4 oops, u need to click the 1st code link,
sample code in C#
This structural code demonstrates the Decorator pattern which dynamically
adds extra functionality to an existing object.
Show code
【在 s*****w 的大作中提到】 : thanks for reply 1st, : my q is from this, : http://www.dofactory.com/Patterns/PatternDecorator.aspx#_self1 : if class Component has a constructor taking 10 parameters, : how to make it work ? : as Decorator is asking for paramters now.
| b*****e 发帖数: 7 | 5 Both the C# samples take a component as iiput parameter.
First sample:
public void SetComponent(Component component)
or second sample:
public Decorator(LibraryItem libraryItem)
There is no need to construct the component in the decorator. By definition
a decorator adds additional behavior to the component *object*, not the
class. So a decorator should always reference the constructed component.
There is no need for a decorator constructor that constructs the component.
【在 s*****w 的大作中提到】 : oops, u need to click the 1st code link, : sample code in C# : This structural code demonstrates the Decorator pattern which dynamically : adds extra functionality to an existing object. : Show code
| s*****w 发帖数: 1527 | 6 Hi Bayside,
i understand most of your replies, but in my case,
class Component has to take parameters for constructors,
so how can i change it ?
the only thing i can think of is constructor is just a dummy constructor,
but call a Component.init(parameters) afterwards.
how do you think ?
definition
【在 b*****e 的大作中提到】 : Both the C# samples take a component as iiput parameter. : First sample: : public void SetComponent(Component component) : or second sample: : public Decorator(LibraryItem libraryItem) : There is no need to construct the component in the decorator. By definition : a decorator adds additional behavior to the component *object*, not the : class. So a decorator should always reference the constructed component. : There is no need for a decorator constructor that constructs the component.
|
|
|
|
|
|
|