vendredi 22 avril 2016

Is there a design pattern that can help me store object "capabilities"?

I am working on a C# program that allows a user to interact with and configure various AV devices such as receivers and switchers over a TCP telnet style connection (according to the command sets of the various device manufacturers). I use the command pattern to encapsulate the commands. The app can only interact with one device at a time, so when it connects it enumerates the device and creates a DeviceVersion object. This object stores certain properties that are common to all the devices. It also needs to maintain a list of capabilities for each device. For instance some devices do not have volume control. Others do, but don't have the ability to change input devices. I hate the idea of storing a long list of booleans inside each DeviceVersion object. I think I should have something like a CapabilityContract object that encapsulates a dictionary such as Dictionary which can map capability names to booleans. At least that way I can put in a ContractFactory or something that can generate those capability objects on the fly, either through text config files or some other method.

However, when it comes to parsing command output, in some cases the capabilities allowed may change how a response is parsed. For instance an AV switch that can separate audio and video on an output may have a GetInput command which returns an ordered pair like (audio input, video input), however, on a device where audio and video are always linked it may just return (input). The command parser interface implementation maintains a reference back to the DeviceVersion, so it can look up the capabilities but that leads to quite a few if statements, or switch statements. I toyed with the idea of generating a different parser implementation for each DeviceVersion type, but that will lead to a lot of duplicated code. Is there some design pattern I am missing that can simplify this problem for me and avoid a lot of either maintaining boolean values, or lots of duplicate code?

Aucun commentaire:

Enregistrer un commentaire