简体   繁体   中英

Shared List between classes

I am in the design stage of my next task and I am not sure whether my idea for it is right or not, as I am not quite sure on how to realize it in an UML diagram. I would appreciate much your comments about it.

Basically the point is that I am going to have a reader and a writer class. They will be used to read and write values from/to an certain data source, ie a database or a modbus PLC. Each of these values is identified by a unique id in my data model and in the data source. The read operation will be performed periodically for all the values by sending all their ids and quering its values. The write operation is made each time one of these values change in my datamodel and needs to be sent to this data source.

My idea is to have a shared List for the reader and the writer containing all the objects in my datamodel. For example:

class ExternalObject {

    private String id;
    private String transactionId;
    private String value;
    private String lastValue;

}

There will be a controller class that when a value changes in my data model will write it in the value attribute of the right object, then the Writer class, that is iterating through all the elements of the list all the time will see that the value is not null and send it. After this, it will reset it to null and set it to the lastValue. Besides, the Reader class, that is reading the values from this data source all the time, when sees that a value read is different from the lastValue, it will save it in my datamodel.

By now I suppose you got the idea. There will be of course some more logics to reset values when there's no connection with the data source or to send the initial values or read them, but that's another thing.

My concern is this shared list. I am not sure if it is fine, in object oriented design, to share lists or objects like this. If this is fine, the next thing is that I don't know how to model it in an UML diagram to indicate that one object is shared between two classes.

Any ideas about it are much welcomed.

Unfortunately this is not a complete answer because I've never implemented anything like that in industry grade, but a few notes come to mind:

1) New IDs: the Reader polls for IDs it knows - but what about new IDs, inserted by external processes?

2) Performance: do you control the schema, and are your machine clocks synchronized within some reasonable margin? If so, perhaps you could have a timestamp on each object, and the reader could 'refresh' only objects that were edited/inserted since its last refresh (plus some safely margin)?

3) List: I wouldn't say "object oriented forbids list sharing", but for your own convenience you might like to consider a wrapper data structure, with methods for searching/updating/inserting/deleting. Thus you can easily replace the datastructure at will, eg to a map.

4) Transactions: how were you going to handle transactions of those data sources?

anyway, good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM