I am using a class (let us call it
ClientImpl) which listens to a socket for messages and then updates a listener to let
ClientImpl know that a message have arrived to the socket. The listener is not implemented inside
ClientImpl, but is passed from the classes using
ClientImpl. Let us call my class
ClientUser to make the problem easier to discuss. An important thing to note is that the
ClientUser has the ownership of the listener.
So to the problem: when a message arrives I want to process it and store it to be able to read later. Since I work with a framework I cannot simply store it in a file. It has to be stored in the heap to be read later.
So the question, "Is it considered bad practice to have a container (array, Collection, ...) inside a listener?". The listener is very specific and will not be used for any other purpose. Else, are there any good options to avoid this?
I have tried to think of some options, but all these would be complicated to implement (read this as: add complexity to the code.). These ways can for example be to start a thread which continously checks for updates in a listener buffer or to make the
ClientUser an Observer. None of these ways are very appealing. Another way would be to inject a dependency to an external Collection. I do not like either because it would create shared responsibility to handle the Collection. This is will likely cause concurrency issues.