🔒

Singleton Pattern in Java

Jul 18, 2024

Singleton Pattern in Java

Introduction

  • Presenter: Navia from Navine Automation Labs
  • Topic: Singleton pattern in Java, common interview question and concept
  • Purpose: Understand and implement Singleton pattern effectively

Key Concepts

Singleton Pattern

  • Ensures only one instance of a class is created
  • Single copy of the object shared among all threads

Steps to Implement Singleton Pattern

  1. Create a private static instance variable
    • Private and static to ensure it belongs to the class
    • Example: private static Browser browserInstance;
  2. Define a private constructor
    • Prevents external instantiation of the class
    • Example:
    private Browser() {}
  3. Provide a public static getter method
    • To return the single instance of the class
    • Example:
    public static Browser getInstance() { if (browserInstance == null) { browserInstance = new Browser(); } return browserInstance; }

Example Code

Browser Class

public class Browser { private static Browser browserInstance; private Browser() {} public static Browser getInstance() { if (browserInstance == null) { browserInstance = new Browser(); } return browserInstance; } public void displayMessage() { System.out.println("Browser info"); } }

Test Class

public class TestBrowser { public static void main(String[] args) { Browser br = Browser.getInstance(); br.displayMessage(); } }
  • Unable to instantiate using new Browser() because of private constructor
  • Use Browser.getInstance() to get the instance

Singleton Pattern in Multi-Threading Environment

  • Multiple threads accessing the method simultaneously
  • Thread Safety: Ensured by adding synchronized keyword

Using Synchronized Keyword

  • Synchronized method approach for thread safety public static synchronized Browser getInstance() { if (browserInstance == null) { browserInstance = new Browser(); } return browserInstance; }
  • Drawback: Performance overhead due to sequential access by threads

Double-Check Synchronization

  • Enhance performance and avoid multiple instantiations public static Browser getInstance() { if (browserInstance == null) { synchronized (Browser.class) { if (browserInstance == null) { browserInstance = new Browser(); } } } return browserInstance; }
  • Add Volatile Keyword: Ensures visibility of changes to variables across threads private static volatile Browser browserInstance;

Practical Example with Multi-Threading

Multi-Threading Test Code

public class BrowserTask implements Runnable { public void run() { Browser br = Browser.getInstance(); br.displayMessage(); } } public class TestBrowser { public static void main(String[] args) { Thread t1 = new Thread(new BrowserTask()); Thread t2 = new Thread(new BrowserTask()); t1.start(); t2.start(); // Join threads to main thread t1.join(); t2.join(); } }
  • Threads: Multiple threads access the getInstance method
  • Synchronization: Ensures only one instance across all threads

Conclusion

  • Purpose: Control object creation, ensuring single instance across application
  • Implementation: Private static variable, private constructor, public static getter method
  • Thread Safety: Synchronized block or double-checked locking
  • Further Usage: Can be integrated with Selenium or other applications

Next Steps

  • Practical implementation in Selenium with Thread Local class for parallel execution (in next video)
  • Goal: Achieve parallel execution properly while maintaining a Singleton instance

Recap

  • Singleton Pattern: Ensure single object instance
  • Key Aspects: Private constructor, static instance, public getter method
  • Thread Safety: Use synchronized block or double-checked locking

Thank you for watching!