DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. When Singleton Becomes an Anti-Pattern

When Singleton Becomes an Anti-Pattern

The Singleton is a useful design pattern, but you have to take care when implementing them, particularly for unit testing. Here are some pitfalls to avoid.

Kapil Bhardwaj user avatar by
Kapil Bhardwaj
·
Oct. 12, 17 · Opinion
Like (8)
Save
Tweet
Share
37.08K Views

Join the DZone community and get the full member experience.

Join For Free

The article below describes the behavior of the Singleton design pattern as an anti-pattern, where it violates the basic principle of creating a single object.

We all are very much aware of Singletons, one of the main Creational Design Patterns. It's a very widely and commonly used Design Pattern. Most of you are aware of the advantages and the problems solved by the pattern, but I am not going to deal with those features in this article.

What most of us fail to understand is that in most of the cases, the Singleton is misused and acts as an anti-pattern. So let's try to look at the scenarios where the Singleton design pattern works as an anti-pattern.

  1. Singletons are basically used as global variables. Using global variables is an enemy of encapsulation because it becomes difficult to define pre- and post-conditions for the client’s object interface. The working of the interface can be handled from within, and not from outside, the interface.

  2. The use of Singletons makes it difficult to unit test classes because classes must be loosely coupled, allowing them to be tested individually.

  3. When unit testing two classes, Singletons break the Single Responsibility Principle because one class is implementing Singleton and other is not. We need to pass in the Singleton as a parameter to the constructor, allowing the tester to mock out the Singleton class easily. The Singleton then does not has to enforce its own singularity. This can be done by factory classes eliminating the global state.

  4. Singletons are bad when used with multi-threading because with just a single object, the options of threading are limited.

  5. Singletons make it easy to break stuff. A file system is usually implemented as a singleton and, mostly, all the file operations are performed under a single method. However, binding the team to use just one common method to implement file operations makes it very difficult to implement.

  6. Singletons decrease performance. If we have a number of lazily initialized Singletons, the compiler cannot fold multiple SingletonDemo.getInstance() calls into one. This is because each call to getInstance() will cause a branch instruction and possibly a cache mismatch.

  7. Singletons promote tight coupling between classes. Singletons tightly couple the code to the exact object type and remove the scope of polymorphism.

  8. Singletons do not complete what they are meant to because a number of techniques — namely Java Reflection, serialization, etc. — are available to create more than one instances of a Singleton class.

  9. Using a static method to initialize Singleton objects is considered a good approach implementing Singletons. But remember, this approach forces the programmer to know the internal code structure of the class, as static methods can be invoked only on class names. Moreover, unit testing static methods is not an easy task using simple Mockito frameworks.

  10. In the Agile world, we need to unit test even a single functionality. One of the most important things about these unit tests is that they must be independent of each other, making it difficult to implement with Singletons.

  11. Extending Singletons is not easy. A programmer needs to use some kind of decorator pattern to change the behavior.

  12. In a garbage collected system, Singletons can become very tricky in regard to memory management.

  13. Singletons can’t be used with clustering.

Conclusion

  1. The use of Singletons should be a very careful and calculated step because they can become a bottleneck during development and debugging.

  2. Instead of using classes, emphasize using enums for implementing Singletons.

Anti-pattern unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Benefits and Challenges of Multi-Cloud Integration
  • Best Practices for Writing Clean and Maintainable Code
  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • Browser Engines: The Crux of Cross-Browser Compatibility

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: