Instruction 1

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

In the previous lesson, you created ContactsBook with the static property, current, so the different modules of the program can all access the same list of contacts. However, nothing in your implementation prevents different teams and team members from using your code to create different instances from ContactsBook. This would cause the program to end up with multiple different contact lists.

Your first thought might be that it’s their fault because they misused your code. You wouldn’t be wrong thinking so, but it’s always safer to secure your code against misuse. You need the class itself to enforce that it can never be instantiated more than once. To do this, you’ll use the appropriately named Singleton pattern.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo 1