【发布时间】:2016-09-28 01:23:22
【问题描述】:
我对模式很陌生,我正在为我必须编写的程序研究装饰器模式。
网上学习,发现了一个装饰器模式的例子(是Java伪代码):
class Solution1
{
static interface Component
{
void doStuff();
}
static class MyComponent implements Component
{
public void doStuff()
{
// ...
}
}
static class ComponentDecorator implements Component // This is the Decorator pattern.
{
private final Component component;
public ComponentDecorator(Component component)
{
this.component = component;
}
public void doStuff()
{
this.component.doStuff();
}
}
static class ComponentDecorator1 extends ComponentDecorator
{
public ComponentDecorator1(Component component)
{
super(component);
}
private void doExtraStuff1()
{
// ...
}
public void doStuff()
{
super.doStuff();
doExtraStuff1();
}
}
static class ComponentDecorator2 extends ComponentDecorator
{
public ComponentDecorator2(Component component)
{
super(component);
}
private void doExtraStuff2()
{
// ...
}
public void doStuff()
{
super.doStuff();
doExtraStuff2();
}
}
public static void main(String[] args)
{
MyComponent c = new MyComponent();
ComponentDecorator1 cd1 = new ComponentDecorator1(c);
ComponentDecorator2 cd2 = new ComponentDecorator2(cd1);
cd2.doStuff(); // Executes Component.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
}
};
当我分析这个例子时,我意识到过去我做了一个非常相似但方式不同的模式:
import java.util.*;
class Solution2
{
static interface Component
{
void doStuff();
}
static class MyComponent implements Component
{
public void doStuff()
{
// ...
}
}
static class ComponentDecorator implements Component // This is NOT the Decorator pattern!
{
private final List<Component> components = new ArrayList<Component>();
public ComponentDecorator()
{
}
public ComponentDecorator addComponent(Component component)
{
this.components.add(component);
return this;
}
public void removeComponent(Component component) // Can Decorator do this?
{
// ...
}
public void doStuff()
{
for(Component c : this.components) c.doStuff();
}
}
static class ComponentDecorator1 implements Component
{
public ComponentDecorator1()
{
}
private void doExtraStuff1()
{
// ...
}
public void doStuff()
{
doExtraStuff1();
}
}
static class ComponentDecorator2 implements Component
{
public ComponentDecorator2()
{
}
private void doExtraStuff2()
{
// ...
}
public void doStuff()
{
doExtraStuff2();
}
}
public static void main(String[] args)
{
ComponentDecorator cd = new ComponentDecorator();
cd.addComponent(new MyComponent());
cd.addComponent(new ComponentDecorator1());
cd.addComponent(new ComponentDecorator2());
cd.doStuff(); // Executes MyComponent.doStuff, ComponentDecorator1.doExtraStuff1, ComponentDecorator2.doExtraStuff2
}
}
在我看来,第二个示例可以在可以使用装饰器模式的相同情况下使用,但它更灵活(例如,您可以删除或重新排序列表中的组件),所以我的问题:
- 解决方案 1(正确的装饰器模式)是否优于解决方案 2?为什么?
- 是否可以在解决方案 1 中添加删除实例的功能?
- 是否可以在解决方案 1 中添加用于重新排序实例的函数?
【问题讨论】:
-
无法分辨哪个更好,因为他们做的事情不同。
-
在我看来他们做同样的事情。你能说得更具体点吗?
-
装饰器是关于通过在扩展代码中包装来扩展另一个实现。您的解决方案只是按顺序调用不同的实现。这是my answer on decorator。尝试使用您描述的替代方法来实现它,您会看到差异
-
请不要在 cmets 中粘贴代码。
-
假设基本的
Stream写入文件,当您调用encryptedZipped.write(data)时,这将如何将加密和压缩数据而不是原始数据写入文件?
标签: java design-patterns decorator