Let's consider another scenario where we implement health check services (such as Nagios) for our infrastructure. We create the HealthCheck
class, which is implemented as a Singleton. We also maintain a list of servers against which the health check needs to run. If a server is removed from this list, the health check software should detect it and remove it from the servers configured to check.
In the following code, the hc1
and hc2
objects are the same as the class in Singleton.
Servers are added to the infrastructure for the health check with the addServer()
method. First, the iteration of the health check runs against these servers. The changeServer()
method removes the last server and adds a new one to the infrastructure scheduled for the health check. So, when the health check runs in the second iteration, it picks up the changed list of servers.
All this is possible with Singletons. When the servers get added or removed, the health...