Exercise: Singleton Library

Introduction

The class Library is the central source of information in a simple library system. If holds lot of methods like

Catalog<Borrower> getBorrowerCatalog() {...}
Catalog<Book> getBookCatalog() {...}
However these methods are of no particular interest in this exercise - so you might just as well avoid them.

How do we ensure that

The answer is: Use the singleton pattern.

Example: SingletonComparer

How to singleton? This is where the exercise really starts

Program the class Library (eager initialization of the singleton)

Lazy initialization

In the above question you used eager initialization: The single instance is created as soon as the class is loaded into the virtual maching

Make another class LibraryLazy where you use the lazy version of the singleton design pattern: The single instance is created when the first client uses the Instance property

equals method

Do you need to override the equals method (from Object) in a singleton class?

The answer is "no"! Next question is why?