另一個替代方案是所述組合物的設計模式,然而,修改,以看起來像「裝飾模式」。
以下示例僅適用於繪圖行爲,不適用於鼠標或鍵盤行爲。忽略這些點,如下圖所示。
注意:它更像C++或C#,請忽略與objective-c的細微差異。
首先,您需要一個將「委託」成員或項目的「繪製」函數的按鈕類。首先讓我們展示主類。
................................................................................
..+--------------------------------+..../|..+--------------------------------+..
..| GraphicObjectClass |.../.|..| CompositeButtonClass |..
..+--------------------------------*--<..|--*--------------------------------+..
..| [+] void Draw(); <<virtual>> |...\.|..| [+] void Draw(); <<override>> |..
..+--------------------------------+....\|..+--------------------------------+..
................................................................................
讓我們假設你想有一個按鈕,:
- 「模塊級」繪製邊框
- 「模塊級」繪製背景,wheter純色或圖案
- 「模塊類」繪製的文本標籤,粗體,斜體,字體名稱
第二個圖,示出了分隔符
................................................................................
..+--------------------------------+............................................
..| GraphicObjectClass |............................................
..+--------------------------------+............................................
..| [+] void Draw(); <<virtual>> |............................................
..+---------------*----------------+............................................
..................|.............................................................
..................^.............................................................
................./.\............................................................
................/...\...........................................................
.............../--+--\..........................................................
..................|.............................................................
..+---------------*----------------+.../|...+--------------------------------+..
..| ButtonComponentClass |../.|...| SolidColorButtonClass |..
..+--------------------------------+-<..|-*.+--------------------------------+..
..| [+] void Draw(); <<virtual>> |..\.|.|.| [+] void Draw(); <<override>> |..
..+--------------------------------+...\|.|.+--------------------------------+..
..........................................|.....................................
..........................................|.+--------------------------------+..
..........................................|.| LabeledButtonClass |..
..........................................|.+--------------------------------+..
..........................................*-* [+] void Draw(); <<override>> |..
..........................................|.+--------------------------------+..
..........................................|.....................................
..........................................|.+--------------------------------+..
..........................................|.| BorderButtonClass |..
..........................................|.+--------------------------------+..
..........................................*-* [+] void Draw(); <<override>> |..
............................................+--------------------------------+..
................................................................................
讓我們的組件集成到按鈕,請注意,我做一個不同的圖表繼承的關聯,從其他協會:該做的separatly E「模塊」或類。
.......................................................................................
.+--------------------------------------------+.....+--------------------------------+.
.| CompositeButtonClass |.....| SolidColorButtonClass |.
.+--------------------------------------------+.....+--------------------------------+.
.| [+] ButtonComponentClass* Background *-----* [+] void Draw(); <<override>> |.
.| |.....+--------------------------------+.
.| [+] ButtonComponentClass* Border *---*....................................
.| |...|.+--------------------------------+.
.| [+] ButtonComponentClass* Label *-*.|.| LabeledButtonClass |.
.+--------------------------------------------+.|.|.+--------------------------------+.
.| [+] void Draw(); <<virtual>> |.|.+-* [+] void Draw(); <<override>> |.
.+--------------------------------------------+.|...+--------------------------------+.
................................................|......................................
................................................|...+--------------------------------+.
................................................|...| BorderButtonClass |.
................................................|...+--------------------------------+.
................................................*---* [+] void Draw(); <<override>> |.
....................................................+--------------------------------+.
.......................................................................................
現在,您將擁有一個按鈕類,它將「委託」成員或項目的「繪製」函數。首先讓我們展示主類。
virtual /* override */ void CompositeButtonClass::Draw()
{
// [Background Draw]
this->Background->Draw();
// [Border Draw]
this->Border->Draw();
// [Label Draw]
this->Label->Draw();
}
只是一個建議。
乾杯。
+1。您可以檢查使用這種方法的VisualWorks Smalltalk GUI框架實現。 – 2013-02-14 13:00:04
+1我已經使用裝飾模式代碼函數,而不是圖形,界面,你的答案,使我必須研究更多關於它;-) – umlcat 2013-02-14 17:55:44