I’m currently working with a program that can interact with a peripheral device that our customers may or may not buy. This device has been a bit of a pain; the company that we are purchasing it from wants our customers to use the company’s own executable to install the device drivers.
However, since not all of our customers will be purchasing the device, it would be useful if they didn’t have to always run a second executable to get our software working. In order to make that possible, I would need to modify our program to work both with and without the drivers.
ActiveX controls are used to interface with the device. These need to be put into the GUI using Windows Form Designer; instantiating the control manually leaves it prone to crashing, irritatingly enough.
Is there any “best” way that one could set up a program to detect missing drivers and avoid crashing? The only ideas that I have thought of are:
- Instantiate the controls manually and use try/catch statements to detect and handle missing drivers, but I don’t think that this solution is feasible.
- Create 2 versions of each form that may use the device. This is a poor idea because it would require careful copying when changes are made on one version of the form.
Typically the best choice in cases like this is to use a Facade or Adapter pattern, often in conjunction with a Factory.
Create an Interface to the device that supports the device not existing and then implement two versions of the interface, one which implements the functionality and one which reports it does not exist.
Then each UI which uses it will call a factory to create the instance object (one of the two choices above as needed) and query that to object via the interface to show controls or surface functionality.
The rest of the UI elements will stay the same and will not need to be maintained in two places.