
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Prevent Reflection from Breaking a Singleton Class Pattern
A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −
Example - Breaking Singleton
import java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester { public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ A a = A.getInstance(); A b = null; Constructor<?>[] constructors = A.class.getDeclaredConstructors(); for (Constructor constructor : constructors) { //make the private constructor as public constructor.setAccessible(true); b = (A) constructor.newInstance(); break; } System.out.println(a.hashCode()); System.out.println(b.hashCode()); } } class A implements Serializable { private static A a; private A(){} public static A getInstance(){ if(a == null){ a = new A(); } return a; } }
Output
705927765 366712642
Here you can see, we've created another object of a Singleton class. Let's see how to prevent such a situation −
Create A using Enum instead of Class.
Example - Protecting Singleton
import java.io.Serializable; import java.lang.reflect.InvocationTargetException; public class Tester { public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ A a = A.INSTANCE; A b = A.INSTANCE; System.out.println(a.hashCode()); System.out.println(b.hashCode()); } } enum A implements Serializable { INSTANCE; }
Output
705927765 705927765
Advertisements