Skip to main content

Object-Oriented Design Patterns: An Introduction

Printer-friendly versionSend to friend
There are a number of books which have been published about patterns in computer science. Certainly the most famous of these is the so-called Gang of Four (GoF) book4. This book also contained early statements about some other core OO design principles including the notion of “programming to the interface” and is recommend reading for any programmer. For the OOABL programmer working on modern distributed architectures, one might also recommend Enterprise Integration Patterns5 as a seminal work for enterprise application architecture. The current document will briefly review the GoF patterns as an introduction to the subject. These patterns will be presented in three categories:
• Creational – patterns for creating objects;
• Structural – patterns for relationships between objects; and
• Behavioral – patterns for the behavior of objects.

Creational Patterns

Abstract Factory
In OO software, a Factory is the place in the code where an object is created. An Abstract Factory is a pattern in which the factories of related objects are combined into an abstract class to separate the creation of objects from their use. A client which needs an object of a particular type uses a generic interface to the Abstract Factory to request an object of a particular type and the Abstract Factory takes the responsibility for creating and delivering that object to the client. The Abstract Factory may be specialized to produce the same object sets with different implementations. E.g., one might have an Abstract Factory to produce documents such as OrderConfirmation, PickingSlip, and Invoice and a subclass of this Abstract Factory might produce different versions of these same documents for internal versus external use. A single interface would be used to request the documents and the correct form would be delivered according to which subclass had been instantiated.

Builder
Builder is a pattern which addresses the problem of constructing complex objects. By decomposing the process of construction into multiple steps, one can use alternate implementations at each step in order to create a complex range of objects from a common basic pattern. Builder differs from Abstract Factory in that Abstract Factory is basically a one step process for related objects while Builder is a multi-step process for complex objects which may be composed of multiple subcomponents. The Builder pattern is typically implemented using an abstract Builder class to provide the interface, a ConcreteBuilder to provide the implementation, and a Director class to manage the structure.

Factory Method
The essence of the Factory Method pattern is to isolate object creation into a method. This can provide several benefits. If the class has subclasses, then each subclass of the base class can override this method to provide the appropriate implementation for that subclass. If there are multiple ways in which the class can be implemented, then using multiple Factory Methods can provide clarity in naming. E.g., if one might create an Order from either database information or from information received in an XML method, one could provide an overloaded constructor in Order with these variations in parameters or one could provide Factory Methods CreateOrderFromDB and CreateOrderFromXML which would make the purpose and context of each clear and unambiguous.

AttachmentSize
OODesignPatterns_20100120.pdf92.3 KB