我正在學習一門考試,這是一個古老的測試:顯示裝飾模式
考慮:
public interface Interface1 {
void method1();
void method2().
}
顯示該接口的Decorator模式的類。
這是什麼,沒有人有任何資源,我可以在這裏瞭解更多?
我正在學習一門考試,這是一個古老的測試:顯示裝飾模式
考慮:
public interface Interface1 {
void method1();
void method2().
}
顯示該接口的Decorator模式的類。
這是什麼,沒有人有任何資源,我可以在這裏瞭解更多?
http://www.javacamp.org/designPattern/decorator.html
http://en.wikipedia.org/wiki/Decorator_pattern
裝飾模式可用於 使它成爲poss能夠在運行時擴展(裝飾) 某個對象的功能 獨立於同一類的其他 實例,提供 一些基礎工作在設計 時間完成。這是通過設計一個 新裝飾類來實現的,這個裝飾類包裝了原始類 。這種包裝可通過的 以下步驟順序來 實現:
- 子類原來的「組件」類成「裝飾」 類(參見UML圖);
- 在Decorator類中,將Component指針添加爲字段;
- 將組件傳遞給裝飾器構造函數以初始化組件指針 ;
- 在Decorator類中,將所有「Component」方法重定向到「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();
}
有一個很好的例子,從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實現的其他模式。
普里特帖子是你的問題
,如果你想了解更多關於設計模式,我強烈建議this post和BalusC答案的答案,它顯示了設計模式,現實生活中的例子
http://userpages.umbc.edu/~tarr/dp/lectures/Decorator.pdf – dbyrne