The Singleton Pattern: One of a Kind Objects
HFDPRead HFDP online
- On the UCSB campus (or any UC campus)
- Off-campus with a UCSBNetID
5: The Singleton Pattern: One of a Kind Objects
TODO: Fill in HFDP_Chapter_5.md with notes for Chapter 5 of HFDP.
5. The Singleton Pattern: One of a Kind Objects (intro, pp.169-170)
Pages 169 and especially 170 provide the basis of understanding what we are trying to do in Chapter 5, so they are worth your time to read.
After reading, can you summarize the main idea (as you understand it so far) of the singleton design pattern?
You might come back and read this again after reading through the chapter once.
The Little Singleton, p. 171-172
This dialog, which extends over two pages (be sure to turn the page from 171 to 172) explores the idea of a private constructor, which is key tool that can be useful in implementing the singleton design pattern.
Side note: This page is making a reference to a book called The Little Lisper, which has a particular style (the style shown on this page.) There are a series of these book, including one called A Little Java, A Few Patterns. For the latter one, if you have access to the Amazon Kindle, you can download a sample chapter.
Dissecting the classic Singleton Pattern implementation (p. 173-174)
Try to understand all the pieces of the code on page 173, and what they do.
Also note that the little box in the upper right hand corner notes: later in the chapter, there will be some flaws pointed out with this code. So be on the lookout for those.
The “interview” on p. 174 is basically a “repeat” of the main points that you should have understood from the material on pp. 169-173. So, if you read through it and it all makes sense, then good. If it is still confusing, then go back and read through 169-173 again.
The Chocolate Factory (pp. 175-176)
This is an exercise (the problem is on p. 176, and the solution to the problem is on p. 188).
Try the problem, and then check your answer. This is a good way to see if you understand the code so far.
And, this could turn into an exam question.