Creational Patterns: Creating Objects in an OO System
- Introduction to Creational Patterns
- Abstract Factory
- Factory Method
Introduction to Creational Patterns
These patterns support one of the most common tasks in object-oriented programmingthe creation of objects in a system. Most OO systems of any complexity require many objects to be instantiated over time, and these patterns support the creation process by helping to provide the following capabilities:
Generic instantiation This allows objects to be created in a system without having to identify a specific class type in code.
Simplicity Some of the patterns make object creation easier, so callers will not have to write large, complex code to instantiate an object.
Creation constraints Some patterns enforce constraints on the type or number of objects that can be created within a system.
The following patterns are discussed in this chapter:
Abstract Factory To provide a contract for creating families of related or dependent objects without having to specify their concrete classes.
Builder To simplify complex object creation by defining a class whose purpose is to build instances of another class. The Builder produces one main product, such that there might be more than one class in the product, but there is always one main class.
Factory Method To define a standard method to create an object, apart from a constructor, but the decision of what kind of an object to create is left to subclasses.
Prototype To make dynamic creation easier by defining classes whose objects can create duplicates of themselves.
Singleton To have only one instance of this class in the system, while allowing other classes to get access to this instance.
Of these patterns, the Abstract Factory and Factory Method are explicitly based on the concept of defining flexible object creation; they assume that the classes or interfaces to be created will be extended in an implementing system. As a result, these two patterns are frequently combined with other creational patterns.