6 Factory Pattern

WHAT’S IN THIS CHAPTER?            

  • What the factory pattern is and why you need it
  • How to implement the various flavors of the factory pattern: the factory method and the abstract factory
  • How to implement the factory pattern in Java EE using the @Producers and @Inject annotations
  • How to create custom annotations and the @Qualifier to disambiguate concrete implementations

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 6 download and individually named according to the names throughout the chapter.

The factory design pattern is one of the widely used core design patterns in modern programming languages. It is used not only by web and application developers, but by the developers of run times and frameworks such as Java and Spring.

The factory pattern has two variations: the factory method and the abstract factory. The intent of these patterns is the same: to provide an interface for creating families of related or dependent objects without specifying their concrete classes. This chapter introduces you to both of these variations and shows you examples of how to implement them.

You will see how the factory pattern has been implemented in Java SE, how that differs from its implementation in Java EE, and how it takes advantage of context dependency injection.

WHAT IS A FACTORY?

As one of the creational patterns, ...

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.