Home CPSC 240

Lab 13: Singletons

 

Objective

To get experience using the Singleton pattern.


 

Task

For this exercise, you will turn a regular class into a singleton class that only allows one instance to be created.

The Logger class provides a way to log messages in three different categories, errors, warnings, and info messages. It allows for directing these three different types of messages to different places. This way, we could choose which go to the screen, a file, or are ignored completely.

This class might be a good candidate to be a singleton. Rather than worry about creating multiple Logger objects, or pass them around to different methods, we could make it a single object.

The LoggerExample.java program provides a main method that tests out this class. You should change these two files so that the Logger class is a singleton.


 

Details

  1. Download the two files and make sure they compile and run.
  2. Change the Logger class into a singleton using the method discussed in class.
  3. Change the main method to get the Logger instance rather than creating a new object.

 

Submitting

When you are done, please submit the Java code under the assignment in Canvas.

Copyright © 2022 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.