0

I have three devices which have different Run() implementations and interfaces. These devices may contain other subsystem or may contain other devices.

I know that I should use Template and Composite pattern. but in template pattern I need to inherit device1 device2 and device3 from device class. On the other hand, in Composite pattern I have device, CompositeDevice and subsystem classes.

I am quite confused about combining these two patterns. Any suggestions ?

4

1 回答 1

0

让我们从http://en.wikipedia.org/wiki/Composite_pattern获取复合模式。Leaf 和 Composite 都继承自 Component 类。现在,要将模板模式与复合模式结合起来,您的所有设备都应该继承 Component 类 - 这些是您的 Leaf 类。最后复合类可以使用你的叶子类。

     Component   ----------
  ^   ^   ^    ^           |
  |   |   |    |           |
Dev1 Dev2 Dev3 Composite<>-|

组件类是包含 Run() 函数声明的接口。

于 2013-08-11T15:25:13.677 回答