CPSC 210

D6: Singleton Pattern

Learning Goals

  • (To review static fields and static methods)
  • To apply the Singleton Design Pattern to a given problem

Recap: Static Fields & Methods

  • A static field is associated with the class rather than with an instance of a class.
    • As a result, every instance of the class shares a variable that is declared static.
       
  • Static fields and methods are fundamental to the
    implementation of the Singleton pattern

Recap: Static Fields & Methods

  • A static method has access only to those fields that are declared static. It cannot access instance data.
     
  • As a result, a static method can be invoked using the name of the class.

Order

Motivation

I want to coordinate a state across my system. For this purpose, I create a class for my state.

How do I make sure there is always only one instance of my State class in my system?

public class WifiState {
  private int numberOfClients;
  private int mbDownloadVolume;
  private int mbUploadVolume;
  
  public WifiState() {}
  
  // setters
}
// ...somewhere in code...
// State state = new WifiState();
// state.setNumberOfClients(30);
// ...
// ...somewhere in code...
// How do I get the current state?

The Singleton Pattern

Gang of Four: "Ensure a class only has one instance, and provide a global point of access to it."

Wikipedia: "Design pattern that restricts the instantiation of a class to one 'single' instance. This is useful when exactly one object is needed to coordinate actions across the system."

The Singleton Pattern

The Singleton Pattern (2)

public class WifiState {
  private int numberOfClients;
  private int mbDownloadVolume;
  private int mbUploadVolume;
  
  public WifiState() {}
  
  // setters
}
public class WifiState {
  private static WifiState INSTANCE;

  private int numClients = 0;
  private int mbDownloadRate = 0;
  private int mbUploadRate = 0;

  private WifiState() {}

  public static WifiState getInstance() {
    if (INSTANCE == null) {
      INSTANCE = new WifiState();
    }
    return INSTANCE;
  }
}

Why not just static instead?

  • We could also implement a global state as a static class
/**
 * Represents a log of alarm system events.
 * We use the Singleton Design Pattern to ensure that there is only
 * one EventLog in the system and that the system has global access
 * to the single instance of the EventLog.
 */
public class EventLog implements Iterable<Event> {

Wait...where have I seen this?

  • Have we not already seen examples on this...?
  • Am I not working with a Singleton right now...?
  • Indeed! Project Phase 4!

Let's have a look...

Problems with Singleton

  • The Singleton pattern breaks OO conventions
    • No abstraction or polymorphism introduced
    • Rather, introduces a new global method
  • Singleton itself breaks SRP:
    • Resp. 1: maintain only one instance
    • Resp. 2: whatever else the class is doing
  • Nowadays often superseded by "dependency injection":
    • Singleton instances are "provided" to you by a framework and you don't use the pattern yourself
  • If you want to learn more, take CPSC 310 😇

Lecture Lab

Highlander (THE ONE HIGHLANDER):
        />___________________________________
[########[]_________________________________/
        />
Highlander (THE ONE HIGHLANDER):
        />___________________________________
[########[]_________________________________/
        />

Desired Output

Highlander (Joe's Highlander):
        />___________________________________
[########[]_________________________________/
        />
Highlander (Sue's Highlander):
        />___________________________________
[########[]_________________________________/
        />

Actual Output

  • Joe and Sue are playing the Highlander game
  • But there are multiple Highlanders

Lecture Lab

D6: Singleton Pattern

The End - Thank You!