General recommendation for design pattern development

I am looking for some tips on the following issue.

I have several classes that are wrappers for many parts of USB equipment - such as programmable bus power supplies, label printers, data acquisition modules, USB to serial converters, etc.

Each of these classes implements the IHardwareDevice interface, defined below ...

public interface IHardwareDevice { string VID { get; } string PID { get; } } // Example IHardwareDevice implementation public class PowerSupply : IHardwareDevice { public string VID { get { return "0123"; } } public string PID { get { return "3210"; } } } 

This interface allows each USB device to specify its own vendor and product identifier.

I also have a static DeviceManager class that uses the SetupDixxx device installation functions to detect the presence of any of the USB devices listed above. This class also has the ability to enable or disable the specified device.

A brief overview of the class is as follows:

 public static class DeviceManager { public static T Find<T>() where T : IHardwareDevice, new() { // Uses the SetupDixxx calls to find a VID and PID match // returns new T() or default(T) depending on whether match was found } public static bool Enable(IHardwareDevice obj) { // ... } public static bool Disable(IHardwareDevice obj) { // ... } } 

I considered using HardwareDeviceAttribute to decorate each class (the attribute must contain VID and PID), but I decided against this.

In addition, I want each of the USB device classes to have its own properties - for example, PortName for a serial USB converter or PathName for a HID device (to allow CreateFile, ReadFile and WriteFile), etc. All of them should be filled out from the values ​​read from the registry branch of individual devices (again, using the calls to the SetupDixxx function). I reviewed the IHardwareDevice interface IHardwareDevice to enable the InitializeDevice method, which will be called by DeviceManager, but this will require access to several unmanaged structures in the hardware class, which seems undesirable. Another option is to decorate the user properties of the USB device, indicating which of them should be filled with DeviceManager .

Now, probably, my question will be - is this or will it be a good implementation of what I want to achieve? Are there any obvious obvious improvements that I'm missing? All the code works, so from the point of view of suitability this is absolutely normal, but is there a more efficient and effective implementation - will the Factory template (or any other) help me in this case?

Thanks in advance, K

+4
source share
1 answer

I'm not very sure, but I think the creator pattern can help you. If you build your object through the builder, you can simply initialize the required number of attributes, leaving the other attributes untouched. wiki link

+1
source

All Articles