4 Singleton Pattern

WHAT’S IN THIS CHAPTER?            

  • The different ways that a developer can implement the singleton design pattern, besides its common usage and pitfalls
  • The problems that using static members and methods causes in multithreaded environments
  • The advances that were made in Java 5 SE with the introduction of the enum type and how it can be used to create thread-safe singletons
  • The use of the @Singleton annotation in Java EE and how this has radically changed the way the singleton pattern is implemented in session beans
  • The use of BEAN- and CONTAINER-managed concurrency and how the @LockType annotation controls access to business methods
  • The main issues that have dogged the singleton pattern and why it is considered an anti-pattern that has fallen out of favor

WROX.COM CODE DOWNLOADS FOR THIS CHAPTER

The wrox.com code download for this chapter is found at www.wrox.com/go/projavaeedesignpatterns on the Download Code tab. The code is in the Chapter 4 download and individually named according to the names throughout the chapter.

The singleton pattern is one of the easiest, most well-known design patterns, but it has fallen out of fashion. Some even consider it as an anti-pattern, which will be discussed later in this chapter. However, enterprise frameworks such as Spring make heavy use of it, and Java EE offers an elegant and easy-to-use implementation. In this chapter, you will see why singletons are needed, why they fell out of fashion, how they can ...

Get Professional Java EE Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.