views:

19

answers:

1

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 current 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
A: 

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.

Space_C0wb0y
So if I disconnect from IR and connect to Serial the factory will change? I am not sure yet whether the business requires the commands to be deleted when you plug into a new NetworkDevice
Schneider