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
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
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 04
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 be useful in Java EE applications, and how you can