Singleton Design Pattern

Overview: Singleton Design Pattern is a creational design pattern that ensure that a class has only one instance while providing a global access point to this instance.

Creational Design Patterns are a design pattern that deal with object creation mechanisms.

When to use Singleton Design Pattern

  • If you have resources which are shared amongst the entire application such as a databases or a file

How to implement

  1. Define a private static attribute in the “single instance” class
  2. Define a public static accessor function in the class
  3. Do “lazy initialization” (creation on first use) in the accessor function
  4. Define all constructors to be protected or private
  5. Clients may only use the accessor function to manipulate the Singleton

REFERENCE:https://sourcemaking.com/design_patterns/singleton

Leave a comment

Design a site like this with WordPress.com
Get started