Singleton Design Pattern in Apex : Amit Chaudhary
by: Amit Chaudhary
blow post content copied from Apex Hours
click here to view original post
In this post/session we will talk about Singleton Design Pattern in Apex and when to use singleton design pattern in Salesforce? This pattern restricts the instantiation of a class to one “single” instance only within a single transaction context. Check our complete Apex Design Patterns training to learn more about design pattern.
What is Singleton Design Pattern in Apex
The Singleton pattern is a creational pattern – it helps you to instantiate objects in a particular way. It allows for the existence of only one instance of an object, but everyone has global access to that object. Singletons can help improve performance and reduce memory footprint.
Singleton design pattern restricts the instantiation of a class and ensures that only one instance of the class exists per transaction in apex. Singleton class must provide a public access point to get the instance of the class for any transaction.
Implement Singleton pattern in Apex
We should consider below point while implementing the singleton pattern in Apex.
- Private Constructor : Create one private constructor to restrict instantiation of the apex class from other classes.
- Private static variable: create private static variable of the same class that is the only instance of the class.
- public method getInstance(): Create one public static method that returns the instance of the class. getInstance method will only initiate instance of class if it does not exist
Singleton Class Diagram
When we should use Singleton Design Pattern?
Singleton pattern can be used for logging and caching related implementation. This pattern can also be used with other design patterns like Abstract Factory and more.
Singleton vs. Static
What’s the difference Singleton and Static?
Singleton | Static |
Singletons can implement interfaces and extend classes | static methods cannot |
Singletons can be instantiated immediately or lazy-loaded | Statics must be instantiated at load |
Singletons follow object oriented principles more closely | Singletons follow object oriented principles more closely than statics. |
Singleton Design Pattern Example in Apex
Here is example of Singleton design pattern in Salesforce.
public with sharing class Singleton {
//private static instance of the class
private static final Singleton instance = null;
//private constructor to avoid creating an instance
private Singleton(){}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
In this pattern, instance will only be instantiated when we call the getInstance() method. This will reduce the effort to load large resources at the time of initialization. Like below code
Singleton instance = Singleton.getInstance();
Singleton Design pattern in Apex Video
Summary
I hope this helped you to understanding Singleton design pattern in Salesforce. Please share your thoughts and suggestions in comments section.
The post Singleton Design Pattern in Apex appeared first on Apex Hours.
September 14, 2022 at 05:30PM
Click here for more details...
=============================
The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment