开发者

Modelling problem - Networked devices with commands

I encountered a head scratching modelling problem today:

We are modelling a physical control system composed of Devices and NetworkDevices. Any example of a Device is a TV. An example of a NetworkDevice is an IR transceiver with Ethernet connection.

As you can see, to be able to control the TV over the internet we must connect the Device to the NetworkDevice. There is a one to many relationship between Device and NetworkDevice i.e. TV only has one NetworkDevice (the IR transceiver), but the IR transceiver may control many Devices (e.g. many TVs).

So far no problem.

The complicated bit is that every Device has a collection of Commands. The type of the Command (e.g IrCommand, SerialCommand - N.B. not currently modelled) depends on the type of NetworkDevice that the Device is connected to.

In the cur开发者_运维技巧rent legacy system the Device has a collection of generic Commands (no typing) where fields are "interpreted" depending on the NetworkDevice type.

How do I go about modelling this in OOP such that:

  • You can only ever add a Command of the appropriate type, given the NetworkDevice the Device is attached to?
  • If I change the NetworkDevice the Commands collection changes to the appropriate type
  • Make it so the API is simple/elegant/intuitive to use


You could use the Abstract Factory Pattern. The idea would be to give the Device a factory for creating Commands. The type of the factory would depend on the type of the NetworkDevice. So, if a Device is connected to an IR-Controller, it will get an IRCommandFactory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜