Posted on by & filed under Design Patterns, java, Learning & Development, programming, Programming & Development, Tech.

Welcome to day three of our seven-day Design Patterns Series. Today, we’ll be learning three Factory patterns, as well as the Singleton pattern. Open a free 10-day Safari trial account to access the series materials.

Factory and Singleton

We’ll begin today by looking at the notion of factories. Then, we’ll look at the Singleton pattern, which provides a way to ensure that only one object of a particular type is created. The reading is a bit longer than usual today, but if you can find some early-in-the-series motivation, it will pay off.


Dive in Head First

  1. Start by reading Chapter 4 from Head First Design Patterns:

    Chapter 4. The Factory Pattern: Baking with OO Goodness

    The Head First chapter begins with “Simple Factories” which are not in the original Design Patterns book but nevertheless are commonly used. It then proceeds to describe the two patterns from the GoF book.

  2. The two factory patterns from the original GoF book are Factory Method and Abstract Factory. You might need to take a break after the Head First reading, but when you’re ready, skim the original descriptions from the GoF book, Design Patterns: Elements of Reusable Object-Oriented Software:

    Class Creational: Factory Method and Object Creational: Abstract Factory

Test Your Knowledge

  1. In general, factories encapsulate ____???____.

  2. True or False: The following guidelines are suggested to avoid violating the Dependency Inversion Principle:

    • No variable should hold a reference to a concrete class.
    • No class should derive from a concrete class.
    • No method should override an implemented method of any of its base classes.
  3. Which factory pattern is most appropriate when you have a family of related objects that are designed to be used together?


Dive in Head First

  1. Start by reading Chapter 5 from Head First Design Patterns:

    Chapter 5. The Singleton Pattern: One of a Kind Objects

  2. Now, skim the original description from Design Patterns: Elements of Reusable Object-Oriented Software.

    Object Creational: Singleton

Test Your Knowledge

  1. What does a Singleton typically look like in Java?

  2. An alternative to creating a Singleton is to put the functionality into static fields and methods on a class. Why might the Singleton pattern be preferable?

  3. True or False: The more design patterns are applied, the better a system’s design is.

Extend Your Knowledge

For more information on factory patterns, including Simple Factories (not in the GoF book), take a look at these other resources on Safari:

There are several variations on the Singleton pattern, and lots of applications. For more information and more ideas, take a look at these resources on Safari:

Tags: design patterns, development, gang of four, java, Learning, programming,

One Response to “Design Patterns Series, Day 3: Factory and Singleton Patterns”

  1. rj

    can you explain why having multiple simple factories doesnt make sense.. i didnt understand that “But you’d like a little more quality control…” pat of factory pattern in that head first design patterns