I’ve been struggling to get my mind around classes. Even after reading several websites and books, I still don’t feel like I really ‘get it’.
The application that I’m working on has a sub that will control a few different pieces of test equipment over a USB to GPIB interface.
So, I have:
Laptop
USB - GPIB Interface
Power Supply (Device Address 5)
HP34970 Datalogger (Device Address 6)
In my sub I would like to Open the GPIB device, send a few SCPI commands to turn on the power supply, send a few more to the HP34970 to switch relays or measure voltage.
Seems simple enough and I can easily make everything work in the same module. However, I think I it would be much better to have a separate class for the GPIB Interface, Power Supply, and HP34970. If this was the case, I could very easily reuse the code in other projects.
Here is where I just can’t get a mental model- If I create an instance of the GPIB class and have a method to ‘open’ a channel to the GPIB bus, how can I allow methods in other classes (like power supply) to use the open channel created in the GPIB class? For example, a method in the power supply class to set voltage.
If someone anyone could take a few minutes to post pseudo code and a little explaination to illustrate how I could / should organize this, I would greatly appreciate the help!
Thanks
Think of classes as devices themselves. The main benefit here would be to be able to reuse code written once – for example, all of your devices have addresses, they can connect, test connection, disconnect – why not have that in one abstract “Device” class, and make all devices inherit from it?
This could be one of the usages – written in C# (sorry if I misunderstood the actual usage 🙂 here I’m assuming powersupply and dataLogger are connected to the interface, and interface to the laptop).
Now, since the interface has the knowledge of the devices it’s interfacing with, it has to have them in it’s constructor (in object oriented design, also known as Dependency injection, more specifically here it’s Constructor Injection):
Basically, if your devices interact with each other in ‘real life’, than it means they should have a reference to each other device they are connected to. For example, if you plugin PowerSupply directly to logger, than the PowerSupply class should have a reference to logger. But, if it’s connected to interface, and then interface to logger, the PowerSupply must have only reference to interface and not the logger (since it’s not connected to it).
I hope you get the idea – it’s not called ‘object oriented’ for nothing – think of classes as real objects – if an object can do thing1, than you should have method ‘thing1()’ in you class; if the object has connection to object441, than the class should have a reference to that class. Just follow this principle and see where it takes you…
When you master that, your program would really benefit from some Inversion of Control (IoC) pattern – that way you just define at the beginning what is a logger, what is powerSupply, who is performing as a display, and whenever someone needs some specific device an IoC container would provide it. This way you don’t have to rewrite code every time there’s a new device in town – just register the new type with IoC container and everything just works.