The Singleton Pattern: One of a Kind Objects

HFDP

Read HFDP online

5: The Singleton Pattern: One of a Kind Objects

https://ucsb-cs56-pconrad.github.io/hfdp/HFDP_Chapter_5/

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.

Singleton Pattern defined p. 177

Houston, Hershey, PA, we have a problem… (pp. 178-179)

Dealing with multithreading (p. 180)

Can we improve multithreading? (p. 181-182)

Meanwhile, back at the Chocolate Factory… (p. 183, top)

Congratulations! (p. 183, bottom)

There are no dumb questions (p. 184-185)

Tools for your Design Toolbox (p. 186)

https://ucsb-cs56-pconrad.github.io/hfdp/HFDP_Chapter_5/