top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Guide to Singleton Design Pattern in Java and its uses in Automation Testing

I was looking for ways to uniquely define some UI resources while doing Automation projects and started exploring more concepts of Java when I came across design patterns. Of course, reading and understanding design patterns is far easier than implementing them and I had to explore a lot, make a lot of notes and my entire collection of notes resulted in this blog. I will explain what I have learned and hopefully, it will be useful for you all


We all know that Java is a high-level Object Oriented programming language. To be honest, I did not quite understand the power of OOP until I came across Design Patterns in Java.


What is a Design pattern?

Design patterns are proven and reusable solutions to commonly occurring problems in software design and development. Java offers various design patterns and we can choose the one as needed for our project design.

They are widely classified as

  • Structural - Here the classes and objects come together to form bigger structures to accomplish several tasks. A good example of a structural design pattern is the Page Object Model (POM) which is widely used in building test automation frameworks. This pattern creates object repositories to store web elements, enhancing test case maintenance and readability and reducing code duplication.

  • Behavioral - Here they define how different objects communicate with each other. "Iterator" is a classic example of a behavioral design pattern. The Iterator objects allow you to traverse through collections of elements, like in Array List, Stack, Hash map, etc

  • Creational - They define and control how objects of a class are created, instantiated, and reused. Singleton is a creational design pattern which we will see in detail.

What is Singleton Design Pattern?

The singleton pattern is a design pattern that ensures that only one instance of a class exists in the application and provides a global access point to it. To quote a real-world example, at any given point in time there can only be one President in the United States of America and he is available to available to everyone.


How Singleton Design Pattern is Implemented?

Let's start with an example. Here I'm creating two classes. A Demo class which is an empty class for now and callDemoClass where in the main method I have created 3 instances for the same demo class and I'm printing the hashcode of each object that is created.







When you run the callDemClass the output will look something like this. It just prints the hashcode of the objects.


As you can see from the above example we have no issues in creating multiple instances of the same class. Suppose you want to restrict this possibilty when you are building frameworks, that's when Singleton pattern comes into play.

Now let's apply Singleton pattern to the Demo class by doing 3 things

  1. Create a private static instance of the class

  2. Create a private constructor

  3. Create a public method to access the object of the class

Now our new demo class code will look like this



This results in an error being thrown in the callDemoClass. Since the Demo class is in Singleton pattern we are no longer allowed to create new instances of the class.





To access the object of the Demo class we have to use the getInstanceMethod(). Let's update the callDemoClass as follows




When you run the callDemoClass the output will look something like this




If you see the output here, the hashcode is printed for each of the objects. Earlier there were multiple instances of the same class. Now that the Singleton design pattern is implemented all the hashcodes are the same, no matter how many instances are created for the Demo class.


How Singleton Design Pattern is relevant in Test automation?

Singleton pattern ensures that only one object of a particular class is created. When you are building a framework, there may be situations where some of the UI resources should have only one instance, have global access, have to be shared across different modules/screens, and maintain consistency.


Some of the examples include

  1. Database connection

  2. File Handling

  3. Font style and colors

  4. Trademark icons and images

  5. Web Driver

  6. Logging


Let's see one more example. I have made some changes to the Demo class and callDemoClass.





When you run the callDemoClass chrome browser window will open and go to the respective websites one after the other from the same window.


Conclusion:

Singleton Pattern is a useful addition to UI automation as it ensures consistency, but we have to exercise caution when using it.


Please like the blog if you found it useful.

Happy learning!!

195 views1 comment

Recent Posts

See All

Selenium Locators

What are Selenium locators? Selenium is a widely used test automation framework. Through Selenium, you can automate any interactions such as type, click, double click with the DOM WebElements, etc. To

bottom of page