【发布时间】:2011-03-12 20:13:37
【问题描述】:
我正在准备考试,这是一个旧考试:
给定:
public interface Interface1 {
void method1();
void method2().
}
显示接口的装饰器模式类。
它是什么,有没有人有任何资源可以让我了解更多相关信息?
【问题讨论】:
标签: java design-patterns decorator
我正在准备考试,这是一个旧考试:
给定:
public interface Interface1 {
void method1();
void method2().
}
显示接口的装饰器模式类。
它是什么,有没有人有任何资源可以让我了解更多相关信息?
【问题讨论】:
标签: java design-patterns decorator
here 有一个很好的例子。
您实际上所做的是创建一个简单版本的 Interface1,然后通过创建装饰(附加类)为其添加更多功能,但始终确保装饰具有相同的界面,从而允许它们在相同的地方使用未装饰的物品。
在上面的链接中,我已经对示例进行了注释。
取一个像window这样的简单类,然后用滚动条装饰它:
// the Window interface
interface Window {
public void draw(); // draws the Window
public String getDescription(); // returns a description of the Window
}
// implementation of a simple Window without any scrollbars
class SimpleWindow implements Window {
public void draw() {
// draw window
}
public String getDescription() {
return "simple window";
}
}
这些是装饰器,首先是一个抽象类,包含装饰器模式的所有通用代码。 - 注意它实现了Window
// abstract decorator class - note that it implements Window
abstract class WindowDecorator implements Window {
protected Window decoratedWindow; // the Window being decorated
public WindowDecorator (Window decoratedWindow) {
this.decoratedWindow = decoratedWindow;
}
public void draw() {
decoratedWindow.draw();
}
}
现在是在窗口中添加垂直滚动条的装饰。请注意,它扩展了 WindowDecorator 并因此扩展了 Window 并因此扩展了界面 Window
// the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
public VerticalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
public void draw() {
decoratedWindow.draw();
drawVerticalScrollBar();
}
private void drawVerticalScrollBar() {
// draw the vertical scrollbar
}
public String getDescription() {
return decoratedWindow.getDescription() + ", including vertical scrollbars";
}
}
Examples of GoF Design Patterns in Java's core libraries 有许多其他用 Java 实现的模式的链接。
【讨论】:
Preet 帖子是您问题的答案
如果您想了解更多关于设计模式的信息,我强烈推荐 this post 和 BalusC 答案,它展示了设计模式的真实示例
【讨论】:
http://www.javacamp.org/designPattern/decorator.html
http://en.wikipedia.org/wiki/Decorator_pattern
装饰器模式可用于 使扩展(装饰)成为可能 特定对象的功能 在运行时,独立于其他 同一类的实例,提供 一些基础工作是在设计中完成的 时间。这是通过设计一个 包装的新装饰器类 原班。这种包装可能是 通过以下序列实现 步骤:
- 将原始“组件”类子类化为“装饰器” 类(参见 UML 图);
- 在Decorator类中,添加一个Component指针作为字段;
- 将组件传递给装饰器构造函数以进行初始化 组件指针;
- 在装饰器类中,将所有“组件”方法重定向到 “组件”指针;和
- 在 ConcreteDecorator 类中,覆盖任何组件方法,其 行为需要修改。
你的例子
public interface Interface1 {
void method1();
void method2().
}
public SimpleInterface implements Interface1 {
void method1() {
//method1 actions
}
void method2() {
//method2 actions
}
}
抽象装饰类
abstract class InterfaceDecorator implements Interface {
protected InterfaceDecorator decoratedInterface;
public InterfaceDecorator (Interface decoratedInterface) {
this.decoratedInterface = decoratedInterface;
}
void method1() {
decoratedInterface.method1()
}
void method2() {
decoratedInterface.method2()
}
}
具体的装饰类
class Method1InterfaceDecorator extends InterfaceDecorator {
public Method1InterfaceDecorator(Interface decoratedInterface) {
super(decoratedInterface);
}
void method1() {
decoratedInterface.method1();
method3()
}
void method3() {
//method3 actions
}
}
用法:
public static void main(String[] args) {
Interface simpleInterface = new SimpleInterface();
Interface decoratedInterface = new Method1DecoratedInterface (new SimpleInterface());
// These two method1 calls will behave differently
simpleInterface.method1();
decoratedInterface.method1();
}
【讨论】: