另一种选择是组合设计模式,但是,它被修改为看起来像“装饰器模式”。
以下示例仅适用于绘图行为,不适用于鼠标或键盘行为。忽略下图中的点。
注意:它更像 C++ 或 C#,请忽略与 Objective-c 的细微差别。
首先,您需要一个按钮类,将“绘制”功能“委托”给成员或项目。让我们首先展示主类本身。
................................................................................
..+--------------------------------+..../|..+--------------------------------+..
..| GraphicObjectClass |.../.|..| CompositeButtonClass |..
..+--------------------------------*--<..|--*--------------------------------+..
..| [+] void Draw(); <<virtual>> |...\.|..| [+] void Draw(); <<override>> |..
..+--------------------------------+....\|..+--------------------------------+..
................................................................................
假设您想要一个按钮,并且:
- 绘制边框的“模块类”
- 绘制背景、纯色或图案的“模块类”
- 绘制文本标签、粗体、斜体、字体名称的“模块类”
第二个图表显示了单独执行此操作的单独“模块”或类:
................................................................................
..+--------------------------------+............................................
..| GraphicObjectClass |............................................
..+--------------------------------+............................................
..| [+] void Draw(); <<virtual>> |............................................
..+---------------*----------------+............................................
..................|.............................................................
..................^.............................................................
................./.\............................................................
................/...\...........................................................
.............../--+--\..........................................................
..................|.............................................................
..+---------------*----------------+.../|...+--------------------------------+..
..| ButtonComponentClass |../.|...| SolidColorButtonClass |..
..+--------------------------------+-<..|-*.+--------------------------------+..
..| [+] void Draw(); <<virtual>> |..\.|.|.| [+] void Draw(); <<override>> |..
..+--------------------------------+...\|.|.+--------------------------------+..
..........................................|.....................................
..........................................|.+--------------------------------+..
..........................................|.| LabeledButtonClass |..
..........................................|.+--------------------------------+..
..........................................*-* [+] void Draw(); <<override>> |..
..........................................|.+--------------------------------+..
..........................................|.....................................
..........................................|.+--------------------------------+..
..........................................|.| BorderButtonClass |..
..........................................|.+--------------------------------+..
..........................................*-* [+] void Draw(); <<override>> |..
............................................+--------------------------------+..
................................................................................
让我们将组件集成到按钮中,注意我为继承关联制作了一个与其他关联不同的图表。
.......................................................................................
.+--------------------------------------------+.....+--------------------------------+.
.| 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();
}
只是一个建议。
干杯。