my problem started when I wanted to write unit test for an abstract class (test the common methods) than I discovered it would be probably better to prefer composition instead of inheritance (I can use mocks for the part which is changing "connection")
Now I have hard time to come up with some design which would be correct so could someone help me here?
Whole my code can be found here but for simplicity I will try to describe it shortly here. I have class PlcData
which represents current status of variables I need which are read from PLC. The PlcData
represents subject in observer pattern and I subscribe to it with different classes (I also need to know what exact attribute has been changed that is why PlcData
implements "source" interface for each attribute I need to observe, I also need to get all data when the attribute is changed because based on some other attributes I search in database - that is why I send whole PlcData
when any attribute changes). I would like to keep the observers of PlcData
independent on connection (how the PlcData
reads/writes variables from PLC).
Here is my original design with abstract PlcData
:
I would like to achieve something like this (a lot of programmers confirmed me that this is the correct way) where the PlcData
is concrete class (I could use also interface for it in future) and the connection is as attribute of PlcData
:
My problem is that I don't know how to correctly implement this. The each concrete class of "Connection" will use some library and will have a lot of specifics here but it should handle the subscription, read, write to those "important" variables (serialNumber, measuredCurve ... - in real code I have more of them but they are exactly given). But I would like this to be invisible for PlcData
"user" so he would just call some factory method PlcDataProvider.getPlcData()
and he would get instance of PlcData with initialised connection and he would just subscribe to variables he needs (or write to those variables and the PlcData
class would handle how to send them to PLC over the "Connection" interface.
MY BIGGEST PROBLEMS
-
I don't know how to correctly make the
Connection
updating thePlcData
and how to write to real PLC variables over it (I am really lost here and I end up with three methods for each attribute (writeSerialNumber, readSerialNumber, subscribeToSerialNumber and theConnection
needs to keep track ofPlcData
) -
How to correctly create PlcData using some factory
Aucun commentaire:
Enregistrer un commentaire