70 likes | 143 Views
PH page 18-24 GoF Singleton p. 127-134. Emanuel Ekstrom. Orphans & Adoption. Context: Making a file system. Three objects: Node, File, Directory. Why do we need to adopt? Why do we need to orphan? Who gets an adopt/orphan interface? So are they all the same?. Singleton. Intent:
E N D
PH page 18-24GoF Singleton p. 127-134 Emanuel Ekstrom
Orphans & Adoption • Context: Making a file system. Three objects: Node, File, Directory. • Why do we need to adopt? • Why do we need to orphan? • Who gets an adopt/orphan interface? • So are they all the same?
Singleton • Intent: ”Ensure a class only has one instance, and provide a global point of access to it.”
Discussion • Who needs only one instance? • What is good about a global access point? • Why not global variables instead?
How do we make a Singleton? (GoF p. 131) • Constructor protected • Public static Instance() function. • Is this enough?
Discussion • What if we want to delete a Singleton? • What if a deleted Singleton is called? • Is there a solution to the problem?
Subclassing • Problem: Making sure the subclasses are unique and that the clients can access it. • How is this solved? • Drawbacks?