【发布时间】:2020-05-13 06:13:57
【问题描述】:
我需要帮助理解这个装饰器示例:
package design.decorator;
public class FillColorDecorator extends ShapeDecorator {
protected Color color;
public FillColorDecorator(Shape decoratedShape, Color color) {
super(decoratedShape);
this.color = color;
}
@Override
public void draw() {
decoratedShape.draw();
System.out.println("Fill Color: " + color);
}
// no change in the functionality
// we can add in the functionality if we like. there is no restriction
// except we need to maintain the structure of the Shape APIs
@Override
public void resize() {
decoratedShape.resize();
}
@Override
public String description() {
return decoratedShape.description() + " filled with " + color + " color.";
}
// no change in the functionality
@Override
public boolean isHide() {
return decoratedShape.isHide();
}
}
这个例子取自这个网站: https://dzone.com/articles/decorator-design-pattern-in-java
我只是不明白他们为什么要费心实现功能不变的方法。例如:
return decoratedShape.isHide();
为什么这是必要的? 在我看来,删除它而不覆盖未更改的方法就可以了。
谢谢。
【问题讨论】: