11 Observer Pattern

WHAT’S IN THIS CHAPTER?            

  • How to implement the observer pattern in plain code
  • How the observer pattern works in the real world
  • How to implement the observer pattern using @Observes and Event firing
  • How to use qualifiers to gain fine-grain control over observers
  • How to implement transaction-sensitive observers and rollbacks

WROX.COM CODE DOWNLOAD 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 11 download and individually named according to the names throughout the chapter.

The observer pattern is one of the most widely used and accepted design patterns in modern programming languages, software, and user interface (UI) frameworks. Most programming languages use observers within their internal application programming interfaces (APIs), and Java is no exception. But Java EE goes further than most and provides a default implementation of the observer pattern, so developers can use this pattern without implementing it from scratch. This chapter focuses on Java’s default implementation of the observer pattern: where it is used, how observers are implemented via annotations in Java EE, and how observers can be made transaction sensitive.

WHAT IS AN OBSERVER?

The idea behind the observer pattern is that an object that changes its state can inform other objects that a change has occurred. In the language of the design pattern, the object ...

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.