I have a queue of uniform message objects with multiple producers and a single consumer. The consumer is publishing the information and needs to be able to grant access based on the data’s origin, so I want the producer send a suitable identifier along with the message. The producers themselves can’t be responsible for the far side access restrictions.
The id should relate to the role of the producer in my application. I want to enforce that all producers have to define one and that a subclassed producer may choose to inherit or redefine it. The producer’s class name would be a good approximation, but the property I want to identify is not really inherent in the class structure, it’s rather something I define.
I could use reflection to find out the class name or maybe an interface name, but this smells of too much overhead. Also, I’m unsure what the appropriate property would be to look for.
As all producers subclass the same abstract parent class, I thought a good way would be to put a constant in there (or in an interface), but then I realised that in Java, a ‘constant’ is really a ‘static final’, which means I can’t override it, so it doesn’t work that way.
How would a more experienced Java programmer do this?
If there’s a ‘type’ that you need to identify, define an interface that forces a class to give you that type, then implement that interface everywhere.
Example:
However, if the list of types is fixed, I would go one step further and make the type an
Enum:And then return this
enuminstead of aString.