显示装饰图案

时间:2011-03-12 20:13:38

标签: java design-patterns decorator

我正在读考试,这是一个旧考试:

假设:

public interface Interface1 {

    void method1();

    void method2().

}

显示界面的Decorator模式类。

它是什么,有没有人有任何资源可以在哪里了解更多信息?

3 个答案:

答案 0 :(得分:4)

here有一个很好的例子。

你本质上正在做的是创建一个简单版本的Interface1,然后通过创建装饰(附加类)向它添加更多功能,但始终确保装饰具有相同的界面,从而允许它们在任何地方使用未修饰的物品。

从上面的链接中,我注释了这个例子。

选择一个像窗口这样的简单类,然后用滚动条装饰它:

// 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实现的其他模式的链接。

答案 1 :(得分:1)

Preet post是您的问题的答案

如果你想了解更多有关设计模式的信息我强烈推荐this post和BalusC回答,它会显示设计模式的真实例子

答案 2 :(得分:0)

http://www.javacamp.org/designPattern/decorator.html

http://en.wikipedia.org/wiki/Decorator_pattern

  

可以使用装饰器模式   使扩展(装饰)成为可能   某个对象的功能   在运行时,独立于其他   提供了同一类的实例   一些基础工作是在设计上完成的   时间。这是通过设计a来实现的   包装的新装饰器类   原班。这种包装可能是   通过以下顺序实现   步骤进行:

     
      
  1. 将原始“Component”类子类化为“Decorator”   class(参见UML图);
  2.   
  3. 在Decorator类中,添加一个Component指针作为字段;
  4.   
  5. 将Component传递给Decorator构造函数以进行初始化   组件指针;
  6.   
  7. 在Decorator类中,将所有“Component”方法重定向到   “组件”指针;和
  8.   
  9. 在ConcreteDecorator类中,覆盖其所有的Component方法   行为需要修改。
  10.   

为你的例子

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();
}
相关问题