1. Technology

Factory Patterns

Delphi OOP Part 13 - Chapter 26

By

Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 25

Introduction

As we mentioned in the introduction to chapter 11, a class’s structure is fixed at compile time.
Since there are occasions when a class needs to take on different characteristics at run time, chapter 11 explored the Template Method and Strategy pattern as techniques to accommodate variations in an object’s behaviour.

This chapter explores the Factory patterns, which, instead of changing an object’s behaviour, provide ways of creating different objects to suit run time requirements. To clarify the context for factory patterns, we’ll quickly review the chapter 11 patterns.

In a Template Method Pattern the client wants:

  1. the result of a particular operation which has fixed steps, but
  2. where the details of individual steps vary depending on context (either between subclasses or between applications), and possibly
  3. where different variations of the steps may have to be added in the future.
In a Strategy Pattern the client wants the result of an entire operation, rather than the individual steps of the operations, to depend on a particular context.

However, in the Factory Method Pattern discussed in this chapter the client wants the actual object that is being instantiated, and not just its behaviour, to depend on context.

In the Abstract Factory pattern, an entire family of objects is created dependent on context.

Factory patterns are widely used, and there are several variations. The examples that follow clarify the general principles and the conditions under which they are appropriate.

A simple Factory Method

A Factory is an important concept in OO programming. Stated briefly, a Factory is an object that creates an unrelated object on behalf of another unrelated object. As the following example shows, the intent of using a Factory is to reduce the coupling between classes, and to make a class more reusable by making it independent of other classes.

Read the full chapter in the provided PDF...

Is it worth the fuss?

Is the Factory Method worthwhile – after all, it adds its own complexity to a system? For a system as limited as this example, there is relatively little motivation to use it. However, as the system becomes more complex, the design issues change. The product classes become more elaborate and there are more objects using them. With the Factory Method, these product users need not concern themselves with all the specifics of the product classes.

Instead this intricacy is encapsulated in a separate class, the Factory, that all the other objects can use. As the size of a program increases, an increase in complexity and coupling is unavoidable. The Factory Method is one way of structuring this complexity and of encapsulating some of it in a specific class where it is easily identifiable and where it is separated from other issues.

Read the full chapter in the provided PDF...

Chapter Contents

  • Example 13.1 A simple Factory Method
  • Ex 13.1 step 1 Without the Factory Method
  • Ex 13.1 step 2 Separation of concerns
  • Ex 13.1 step 3 The Factory Method
  • Ex 13.1 step 4 Using the Factory
  • Ex 13.1 step 5 An alternative selector
  • Ex 13.1 step 6 Modifying the Factory Method
  • Ex 13.1 step 7 Class diagram for the Factory Method
  • Ex 13.1 step 8 Managing object lifetimes
  • Is it worth the fuss?
  • Example 13.2 The Abstract Factory Pattern
  • Ex 13.2 step 1 The Tap Factory
  • Ex 13.2 step 2 The product ranges: the Bath Tap
  • Ex 13.2 step 3 The kitchen taps
  • Ex 13.2 step 4 The Abstract Tap Factory
  • Variations on the Factory Method
  • Pattern 13.1 The Factory Method Pattern
  • Pattern 13.2 The Abstract Factory Pattern
  • Homework Problems

Chapter Download

Here's the full text of the Delphi OOP part 13 available as PDF, along with the Delphi source code examples:

©2014 About.com. All rights reserved.