Design Patterns
there are three main categories of Design Patterns as decreed by the ‘Gang of Four’1 (the authors of a seminal work Design Patterns | Elements of Reusable Object-Oriented Sofware.
| Creational | Structural | Behavioural |
|---|---|---|
| 1. Factory Method | 6. Adapter | 13. Interpreter |
| 2. Abstract Factory | 7. Bridge | 14. Template Method |
| 3. Builder | 8. Composite | 15. Chain of Responsibility |
| 4. Prototype | 9. Decorator | 16. Command |
| 5. Singleton | 10. Façade | 17. Iterator |
| 11. Flyweight | 18. Mediator | |
| 12. Proxy | 19. Memento | |
| 20. Observer | ||
| 21. State | ||
| 22. Strategy | ||
| 23. Visitor |
Creational
provide object creation mechanisms that increase flexibility and reuse of existing code
Factory Method
provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
structure
- the product declares the interface, which is common to all objects that can be produced by the creator and its subclasses
- concrete products are different implementations of the product interface
- creator class declares the factory method that returns new product objects.
- note, the return type of this method must match the product interface
- concrete creators override the base factory method so it returns a different type of product.
- note that the factory method does not have to create new instances all the time! it can also return existing objects from a cache, an object pool, or another source.
- this is helpful for resource intensive objects such as database connections, file systems, and network resources
- note that the factory method does not have to create new instances all the time! it can also return existing objects from a cache, an object pool, or another source.
relations with other patterns
- if the base factory method becomes empty, you can make it abstract
- many designs start be using Factory Method (less complicated and more customisable via subclasses), and then evolve toward Abstract Factory, Prototype, or Builder (more flexible, but also more complicated).
- pairs well with Iterator
- prototype is not based on inheritance. factory method is.
- factory method is a specialisation of template method.
- factory method may serve as a step in a large Template Method.
Abstract Factory
lets you produce families of related objects without specifiying their concrete classes
Builder
lets you construct complex objects step-by-step. the pattern allows you to produce differente types and representations of an object using the same construction code.
Prototype
lets you copy existing objects without making your code dependent on their classes.
Singleton
lets you ensure that a class has only one instance, while providing a global access point to this instance.
Structural
assemble objects and classes into larger structures, while keeping the structures flexible and efficient
Adapter
Bridge
Composite
Decorator
Façade
Flyweight
Proxy
Behavioural
allow effective communication and the assignment of responsibilities between objects
Interpreter
Template Method
Chain of Responsibility
Command
Iterator
Mediator
Memento
Observer
State
Strategy
Visitor
-
Ralph Johnson, Richard Helm, Erich Gamma, and John Vlissides. ↩︎