2011-03-12 102 views
0

我正在学习一门考试,这是一个古老的测试:显示装饰模式

考虑:

public interface Interface1 { 

    void method1(); 

    void method2(). 

} 

显示该接口的Decorator模式的类。

这是什么,没有人有任何资源,我可以在这里了解更多?

+0

http://userpages.umbc.edu/~tarr/dp/lectures/Decorator.pdf – dbyrne

回答

0

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

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

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

  1. 子类原来的“组件”类成“装饰” 类(参见UML图);
  2. 在Decorator类中,将Component指针添加为字段;
  3. 将组件传递给装饰器构造函数以初始化组件指针 ;
  4. 在Decorator类中,将所有“Component”方法重定向到“Component”指针 ;和
  5. 在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(); 
} 
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"; 
    } 
} 

这些是装饰,第一一个抽象类来与装饰图案的所有共同的代码。 - 请注意,它实现了窗口

// 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从而窗口,因此在界面窗口

// 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

普里特帖子是你的问题

,如果你想了解更多关于设计模式,我强烈建议this post和BalusC答案的答案,它显示了设计模式,现实生活中的例子