Pete855217
Pete855217

Reputation: 1602

Appropriate Class structure representing a hardware device

I'm trying to code up an interface to a basic ECG device. I need the form to setup the device, send the 'record' message and save the ECG data that comes back into a file (as well as report a bit on the screen). The hardware device gets sent commands, and returns data via a serial interface.

My question is about the most appropriate class structures to set up.

Option 1: MainWindow instantiates a Hardware Device Object that reads the ECG info realtime, creates an 'ECG File class object' and handles it all internally to the Device object. When recording is finished, MainWindow deletes the Device object and we're done.

Option 2: MainWindow instantiates the Hardware Device Object that receives a whole lot of data, maintains that data as a publically accessible data structure (member) then MainWindow would then refer to that Device Object data structure, instantiate the ECG File class object itself and write it out to a file.

Ideally I'd like to write the data out in different formats (eg. classes that specify the format?)

Sorry if the question's not that clear, I guess I'm asking whether it's appropriate for a hardware device object to also manage all its own data, or pass it back for the main window to then process itself.

I've had a go at option 1, but it's getting ugly and I'm not sure whether I've mis-designed the thing from the start.

Any/all views appreciated!

Pete

Upvotes: 0

Views: 334

Answers (1)

Christopher Langmead
Christopher Langmead

Reputation: 26

Not knowing too much about the Domain, I have designed several systems that uses devices. I have also seem some design for devices. There are many ways to design this kind of stuff, but i like to write wrappers for all devices and use an open, close, read, and write interface.

So essentially, an abstract class is created called Device. Specific devices can be designed to extend this base class. Then a builder class is created to create and initialize the specific class. From this basic class a system can be built to handle it's out input and output. I tend to design to interfaces and keep classes as simple as possible.

Upvotes: 1

Related Questions