Join Elisabeth Robson for an in-depth discussion in this video Understanding the classic singleton pattern, part of Foundations of Programming: Design Patterns.
- View Offline
Here, I'm instantiating my singleton object. I take that. I can use it over and over. And I can just call it my singleton. Well, the obvious problem with that is, any other object can come along and do the same thing. And then you end up with a second instance. And so, it's no longer a singleton. And so, just by using typical Java classes and instantiating them. That gives us absolutely no protection against other objects instantiating them too. Let's think a little more about how we might prevent other objects from doing that instantiation. We're going to do this step by step.
And while it might not be obvious at first, there is a method to our madness. And we're going to start by observing that one thing we can do. Is create a Java class with a private constructor. Now you might first ask, is that legal. And it is. And you might also ask. How am I going to instantiate it? And the answer is, only code in this class can actually call the private constructor to instantiate it. And while it won't be obvious yet, that's one of the keys to implementing this pattern. Sad again, only code that lives in the singleton class can call this private constructor.
Now let's make use of that fact, to write some code that calls the constructor. Like this. So here we have a static method, getInstance, that actually instantiates a class singleton, and further, once it's instantiated, it's returned to the caller. So notice, we've created a way to control the instantiation of this class, which right now has to come through the getInstance method. But so far, every time it's called, we're just returning a new object. Not a singleton object. But we can take this further. Here, we've added a private static variable unique, whose type is Singleton, and now we're assigning the new instance of Singleton to the variable the first time we've created it.
Now, the second time we call getInstance, if that instance already exists, then we just return one we've already made, ensuring that there's only one instance. Of Singleton. This is the Singleton pattern. We've created one class that ensures there's only ever one instance. And, we used the Guiness's method to get access to that incidence. The class diagram is simple. Just one class with a getInstance method. Now if you think you've got the Singleton pattern down, and you can stop the chapter here, keep going. because there's a lot of caveats to how this is implemented.
But for right now. This is the canonical way to implement Singleton Java. And we'll see all the caveats coming up.
- What are design patterns?
- Encapsulating code that varies with the strategy pattern
- Setting behavior dynamically
- Implementing the observer pattern
- Creating chaos with inheritance
- Extending behavior with composition
- Dealing with multithreading and the singleton pattern
- Revising the design for a state machine
- Encapsulating iteration with the collection pattern
- Encapsulating object creation with the factory method pattern