By Tony Bevis on Sunday, 22 October 2023
Category: Java

Java programming course: 15.2 Making ZooAdministrator a singleton class

Making ZooAdministrator a singleton class

You have previously learned that to instantiate an object of any class you use the new keyword to invoke a class's constructor. For example, to instantiate some animals you may have done this:

Given that you only want one instance of ZooAdministrator, what is to prevent a class from inadvertently doing this?:

Above, admin1 and admin2 reference two separate ZooAdministrator objects, each with their own copy of any contained data. If this data is modified in one of these objects, then the other will be out of step. As it stands, any client object can instantiate a ZooAdministrator object because its constructor is define as public. There are three steps involved to prevent this:

Step 1: make the ZooAdministrator constructor private

With a private constructor only the ZooAdministrator class itself will be able to instantiate its own objects. You now need to declare a static variable to store a reference to one such object.

Step 2: define a static variable for the singleton

Inside ZooAdministrator declare the following static variable:


You now need a static method to provide the single instance of ZooAdministrator:

Step 3: define a static method for the singleton

Inside ZooAdministrator declare the following static method:


With the above changes in place the only way for client objects to create a ZooAdministrator object is through the above method:

Above, admin1 and admin2 both refer to the same ZooAdministrator object, so there is therefore only one set of data that needs to be managed.

At this stage you can change the code inside the main() method of VirtualZoo to obtain the ZooAdministrator object in the above way:

Communication between the user interface and the core system

Since the graphical application you will develop will add, change or remove zookeepers, there needs to be a way of storing a collection of existing ZooKeeper objects and enabling the user interface to communicate with that collection.

Currently in the ZooAdministrator class is defined the following:


Before developing the user interface you will modify the ZooAdministrator class in the following ways:

  1. Use a collection to store any number of ZooKeeper objects
  2. Remove the createExampleZooKeepers() method since by the end of the next section you will be able to create zookeepers using the user interface (you should also remove method createExampleResponsibilities() since it will no longer have access to the sample zoo keepers)
  3. Provide a method that will add a new zookeeper to the collection
  4. Provide a method that will change an existing zookeeper's details in the collection
  5. Provide a method that will remove an existing zookeeper from the collection
  6. Provide a method that will return the collection of zookeepers

​Step 1: use a collection

In ZooAdministrator replace the following instance variable declaration:

With this:

Step 2: don't create the example zookeepers

Delete the createExampleZooKeepers() and createExampleResponsibilities() methods and their invocation from within the constructor. You also need to instantiate an empty collection for the zookeepers (which can be a HashSet) and the responsibilities map. The constructor should now look as follows:

Step 3: define a createZooKeeper() method

Define a createZooKeeper() method that requires the appropriate data as its arguments, instantiates a ZooKeeper object, adds it to the collection, and returns it:

Step 4: define a changeZooKeeper() method

Define a changeZooKeeper() method that requires an existing ZooKeeper object and its new data as its arguments, modifying the provided object:

Step 5: define a removeZooKeeper() method

Define a removeZooKeeper() method that requires a ZooKeeper object as its argument and removes it from the collection:

Step 6: define a getZooKeepers() method

Define a getZooKeepers() method that returns the collection of zookeepers:

Next lesson: 15.3 Modify the other core classes

Related Posts

Leave Comments