Just looking for some advice regarding a structure that I am using in some message processing code.
The messages that I am processing are essentially Strings with various “segments”. I have written some classes to process the segments (one segment type per class). The segment readers are extensions of an Abstract class that includes common methods. Finally, each segment reader has public as well as private methods. Now, the first version of the code passed the message to the constructor of each segment reader as it was required:
SegmentReaderType1 r = new SegmentReader1(message)
String item = r.getSomethingInteresting()
This seems very clumsy – so I tried (using static methods):
String item = SegmentReaderType1.getSomethingInteresting(message)
This seems cleaner, however the some readers are complex internally so there is a lot of passing (internally) of message (I have no idea if this is good or bad?).
As an alternative, I have been wondering if there is a mechanism whereby I could “share” the message from the abstract parent to the readers? Maybe a singleton MessageHolder would be a better choice?
The code works, however I am really trying to develop better coding techniques. Any assistance appreciated
Having a static field and a property is the best choice in your case. You cannot use singleton with SegmentReaderType1 class because it is not possible to instantiate abstract classes. And to a make another class just for holding the message, in my opinion, is too much.