How to create a Singleton Pattern?

Blog
Spread the love

This blog we will be learn How to create a Singleton Pattern?

Introduction about Singleton Design Pattern.

  • The Singleton design pattern restricts the instantiate of a class to one object.
  • Class has only one instance and that instance can be accessed globally in a application.
  • Useful to restrict the limited no. of connections in a database or limited amount of memory for a particular instance of class.

How to create a Singleton Pattern?

  • Static member variable – It acts as a placeholder for the instance of that class
  • Private Constructor – Private constructor prevents direct instantiate of a class.
  • Prevent object cloning by making clone magic method private (__construct(), __destruct(), __toString(), __get(), __set(), __call() etc).
  • Create a static method for global access.

How it will work:

  • We have to create one static method, that static method should return the single instance of the class.
  • Instance will create for this class when we calling the time of this method.
  • Created instance save into one private static variable & this variable returns the instance whenever you want.
  • Second time on wards whenever you want it will fetch it from this static variable & it will return instead of create again.

Real Time Sample Example:

Ex:1 Database creation in PHP:

Consider we want to fetch some values from Database more than 10 times (approx.). In this case each & every time if you create a database connection (approx. 10 times) and contacting a DB is not a correct way. Due to this more memory will take & off-course more effort will have gone. To avoid this, better create one instance for connecting the DB & use till that times (approx.). Whenever you are call this method check whether your variable is having the active instance or not. If there the use same else create new instance (DB connection code).

Ex: 2 Customer Login

In all the website whenever you are login as a customer that customer object is active till logout. We case use that object/instance to get the customer detail with the session.

Disadvantage:

  • Not able to inherit
  • We can’t clone the instance. If you clone or copy the instance, then singleton design pattern is end in that application. We cannot use that instance object globally.
  • Second instance we couldn’t able to create.
bluethinkinc_blog

bluethinkinc_blog

2022-11-16

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

Enhancing Productivity with Dynamics 365: The Power of Session Restore

Spread the love Enhancing Productivity with Dynamics 365:

Salesforce Admin

Spread the love In our last blog, we discussed common

Unlocking AI with Spring Boot: Real-World Use Cases and Solutions

Spread the love Artificial Intelligence (AI) has moved from

Enhancing Performance with Reactive Java: A Beginner-Friendly Guide

Spread the love In today’s fast-paced software

Integrating Azure Function with Dynamics 365

Spread the love Signup on azure portal and make sure to

How to Create a Power Apps Component Framework (PCF) with React

Spread the love If youʼve been looking to supercharge your

Customize lead qualification for a seamless experience

Spread the love When the 2024 Release Wave 2 was launched,

Create a Custom Console Command in Spryker

Spread the love In Spryker, developers can create custom

SALESFORCE

Spread the love Salesforce is a cloud-based software

CRUD Operation in Spryker Backoffice with a Custom Module

Spread the love Spryker is a highly flexible and modular

bluethinkinc Blogs