Simply put, this is another place. But is this logical for your situation? You should ask yourself that.
It might be better to leave ConnectionMonitor mutable. He is responsible for “monitoring” the connection, so he is required to keep track of values that may change. Otherwise, you will need another object that you can modify to track this state.
If this is not convincing enough, then here are a few ways:
You may have a container class for your monitors that maps ConnectionMonitors to it ConnectionState:
class MonitorManager { Map<ConnectionMonitor, Boolean> connectionStatuses = ...; }
To make this simple, you can transfer this manager to each monitor, allowing the listener to access the map and change the logical value for this connection:
class ConnectionMonitor { private MonitorManager manager; //.... private void processConnectionMessageContent(final String messageContent) { if (messageContent.contains("Disconnected")) { logger.warn("Disconnected message received!"); manager.connectionStatuses.put(this, false); } else if (messageContent.contains("Connected")) { logger.info("Connected message received."); manager.connectionStatuses.put(this, true); notifyOnConnect(); } } }
But some developers have lined up to let you know that children do not need to know about their containers.
Create a new object that is responsible for the data collected during connection monitoring:
class MonitorManager { private Map<ConnectionMonitor, MonitoredData> data = ...; public void createMonitor() { MonitoredData data = new MonitoredData(); this.data.put(new ConnectionMonitor(data), data); } } class ConnectionMonitor inplements MessageConsumer { private MonitoredData data; public ConnectionMonitor(MonitoredData data) { this.data = data; }
The details in MonitoredData may be better suited for the object being monitored. It would be easier to help if you provided more context.
Vince emigh
source share