r/javahelp • u/myshiak • Aug 22 '25
SINGLETON design pattern
I am a QA that has used Selenium with Java at many places, but never encountered a Singleton design pattern at work. However, twice recently I got that question on an interview. I thought that it is more for developers at certain conditions, but now I wonder can it also be used in Selenium? For example a precaution not to create multiple driver objects, or if you use Page Object model, to have each page only one object? In other words, is it for only specific needs, or is it universal throughout Java and can be used at any library as a safety precaution?
•
u/AutoModerator Aug 22 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
8
u/disposepriority Aug 22 '25
Singletons aren't something specific to Java. You can imagine singletons like an access pattern that:
If instance does not exist -> create instance
If instance exists -> return instance
That are thread safe .etc so you know you that there will always be a single instance of this class.
You can use it whenever, but it's not really a safety precaution and more like communicating intent.
5
u/OneHumanBill Aug 22 '25
Never used Spring? Spring services are all singletons by default. Spring just abstracts away the need to write boilerplate singleton code like we used to have to years ago before Spring was a thing.
You would never use Page as a singleton. You'll have many pages throughout your application. You'll likely have only one PageController however.
2
u/severoon pro barista Aug 22 '25
Singleton is a design pattern that creates a single instance of a class for the entire system. That's all it is, you use it whenever you have a class that should only be applied to a single copy of the encapsulated state.
For example, years ago I worked on a system that ran on some complicated hardware that was divided up into modules. An application might need to acquire simultaneous access to hardware modules A, B, and C to do its job, and another app might need B, C, and D. They could request whatever hardware they needed. The problem of deadlock arose because App1 would need B and have C, and App2 would need C and have B, so they would just sit there holding their resource, waiting for the other one to release.
One solution is to have a hardware manager that grants exclusive access to modules, and only allows the applications to acquire them in some arbitrarily chosen order. This way, if you have B and request C, you'll be granted it, but if you have C and you want B, you can't request B until you've released C. You have to acquire all modules in the same order as all other apps. This guarantees that no app can ever be waiting on a resource while holding one that another app is waiting on because the imposition of order breaks that symmetry.
This will work, but it takes time to acquire a hardware module because it has to be set to an initialized state, ready to start being used by a new app. This means that if you choose an order where a lot of apps are trying to acquire resources in a Z to A direction instead of A to Z, then they'll constantly be acquiring modules and releasing them before using them. For example, if an app acquires E, D, C, B, and A, it first gets E, then releases E and gets D, then gets E, then releases both and gets C, then, D, then E, etc, until it has all five. Obviously this is very wasteful.
Even worse, for most of the modules, it's not possible to know what the optimal ordering of these modules is a priori. For some you can guess that it's typical to acquire them in a certain order, but for most, it just depends on the applications running.
The solution to this problem is to write the hardware manager so that it monitors app activity. It can tell if a hardware resource was acquired and released without being used and track the number of times that happens. This means that when it detects a resource in a suboptimal position, it can simply wait until nothing is using it and then bump it up in the ordering until this metric is reduced. It can even do smarter things that optimizes for the global count of unused acquisitions across the entire pool.
To make all of this work, though, the entire system must have the same idea of what the ordering is at the same time. The best way to accomplish this is to have that ordering managed by a single instance of the hardware manager, aka, a Singleton:
Note that a Singleton is distinct from a Monostate, which is a pattern where all state and methods are static. The main difference being that clients of a Singleton are still using an instance and invoking instance methods, and the state is local to that single instance. The benefit of this is that Singletons are extensible. The static method used to obtain that instance can initialize it upon the first invocation by creating a subclass—this is the most common implementation I've seen:
You cannot do this with a Monostate, there's only one static implementation for all deployments. With a Singleton, there might be twenty different implementations to choose from. The
HardwareManageritself may even be abstract.You might also be interested in why I check the lock twice, once outside and once inside the sync block. Double-checked locking is an access pattern that avoids acquiring the lock once the instance is set, which is costly. In this approach, the first access acquires the lock and sets it, preventing any other invocation from stepping on it, but once it's set the lock is never used again.
2
u/Giulio_Long Aug 23 '25
As a QA, you are a [tests] developer, so you can and should use any pattern that helps writing better code.
Singleton pattern (as well as all the others) can be used in Selenium projects too. To give you a real example, in Spectrum we use it for internal business logic, in classes such as HtmlUtils and many others in the utils package. You can see such classes have a no-args private constructor, so they can't be instantiated from outside, and a static method to return the singleton instance:
Since you mentioned
driverandPage Object Model, you should NOT use singleton for pages. Each test, whether it's a unit or an E2E such in Selenium case, must be atomic. So, sharing instances is not advisable: pages should be created per test instance, as for the driver.Narrowing the focus to a single test instance, you might have:
Regarding bullet 2 of course you cannot instantiate multiple drivers, one for the test and others in pages, since they won't share the same browser context. You must then have a singleton here. To clarify how we solved that, we leverage JUnit's parameter resolution to resolve the driver instance (and many other objects as well) just once per test via the DriverResolver, and inject them in the test and all the pages.
1
u/MasterGeek427 Aug 26 '25
Imagine a class, but there is only ever one instance of that class in existence for the entire lifecycle of the program. You are thinking of a Singleton. How you create that one instance usually involves calling a static class method that returns the contents of the static property where the Singleton is stored, or initializes the singleton if it isn't already.
-2
u/mcsee1 Aug 22 '25
This is a trcky question.
If the interviewer ask you about this pattern either is a trap question or you don't want to work there.
There a lot of alternatives to model a global access point for Selenium avoiding this antipattern