The simplest and most common patterns:
Abstract Factory
Adapter
Composite
Decorator
Factory Method
Observer
Strategy
Template Method
It's hard to find an object-oriented system that doesn't use at least a couple of these patterns, and large systems use nearly all of them. This subset will help you understand design patterns in particular and good object-oriented design in general
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
CCOR
1. Creating an object by specifying a class explicitly.
3. Dependence on hardware and software platform.
4. Dependence on object representations or implementations.
6. Tight coupling.
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
CCOR
8. Inability to alter classes conveniently.
Decouple an abstraction from its implementation so that the two can vary independently.
CCOR
3. Dependence on hardware and software platform.
4. Dependence on object representations or implementations.
6. Tight coupling.
7. Extending functionality by subclassing.
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
CCOR
5. Algorithmic dependencies.
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until an object handles it.
CCOR
2. Dependence on specific operations.
6. Tight coupling.
7. Extending functionality by subclassing.
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
CCOR
2. Dependence on specific operations.
6. Tight coupling.
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clientstreat individual objects and compositions of objects uniformly.
CCOR
7. Extending functionality by subclassing.
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
CCOR
7. Extending functionality by subclassing.
8. Inability to alter classes conveniently.
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
CCOR
6. Tight coupling.
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Methodlets a class defer instantiation to subclasses.
CCOR
1. Creating an object by specifying a class explicitly.
Use sharing to support large numbers of fine-grained objects efficiently.
Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
CCOR
5. Algorithmic dependencies.
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
CCOR
6. Tight coupling.
Without violating encapsulation,capture and externalize an object's internal state so that the object can be restored to this state later.
CCOR
4. Dependence on object representations or implementations.
Define a one-to-many dependency between objects so that when one object changes state,all its dependents are notified and updated automatically.
CCOR
6. Tight coupling.
7. Extending functionality by subclassing.
Specify the kinds of objects to create using a prototypical instance, andcreate new objects by copying this prototype.
CCOR
1. Creating an object by specifying a class explicitly.
Provide a surrogate or placeholder for another object to control access to it.
CCOR
4. Dependence on object representations or implementations.
Ensure a class only has one instance, and provide a global point of access to it.
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
CCOR
5. Algorithmic dependencies.
7. Extending functionality by subclassing.
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
CCOR
5. Algorithmic dependencies.
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
CCOR: 5. Algorithmic dependencies.
8. Inability to alter classes conveniently.
Common causes of redesign |
Design patterns |
Abbr. |
1. Creating an object by specifying a class explicitly. |
Abstract Factory , Factory Method , Prototype |
FAP |
2. Dependence on specific operations. |
Chain of Responsibility , Command |
CC |
3. Dependence on hardware and software platform. |
Abstract Factory , Bridge |
BA |
4. Dependence on object representations or implementations. |
Abstract Factory , Bridge , Memento , Proxy |
BAPM |
5. Algorithmic dependencies. |
Builder , Iterator , Strategy , Template Method , Visitor |
BITVS |
6. Tight coupling. |
Abstract Factory , Bridge , Chain of Responsibility , Command , Facade , Mediator ,Observer |
BACCOFM |
7. Extending functionality by subclassing. |
Bridge , Chain of Responsibility ,Composite , Decorator , Observer , Strategy |
BCCDOS |
8. Inability to alter classes conveniently. |
Adapter , Decorator , Visitor |
ADV |
Here are some common causes of redesign along with the design pattern(s) that address them:
1. Creating an object by specifying a class explicitly. Specifying a class name when you create an object commits you to a particular implementation instead of a particular interface. This commitment can complicate future changes. To avoid it, create objects indirectly.
Design patterns: Abstract Factory , Factory Method , Prototype .
2. Dependence on specific operations. When you specify a particular operation, you commit to one way of satisfying a request. By avoiding hard-coded requests, you make it easier to change the way a request gets satisfied both at compile-time and at run-time.
Design patterns: Chain of Responsibility , Command .
3. Dependence on hardware and software platform. External operating system interfaces and application programming interfaces (APIs) are different on different hardware and software platforms. Software that depends on a particular platform will be harder to port to other platforms. It may even be difficult to keep it up to date on its native platform. It's important therefore to design your system to limit its platform dependencies.
Design patterns: Abstract Factory , Bridge .
4. Dependence on object representations or implementations. Clients that know how an object is represented, stored, located, or implemented might need to be changed when the object changes. Hiding this information from clients keeps changes from cascading.
Design patterns: Abstract Factory , Bridge , Memento , Proxy .
5. Algorithmic dependencies. Algorithms are often extended, optimized, and replaced during development and reuse. Objects that depend on an algorithm will have to change when the algorithm changes. Therefore algorithms that are likely to change should be isolated.
Design patterns: Builder , Iterator , Strategy , Template Method , Visitor .
6. Tight coupling. Classes that are tightly coupled are hard to reuse in isolation, since they depend on each other. Tight coupling leads to monolithic systems, where you can't change or remove a class without understanding and changing many other classes. The system becomes a dense mass that's hard to learn, port, and maintain.
Loose coupling increases the probability that a class can be reused by itself and that a system can be learned, ported, modified, and extended more easily. Design patterns use techniques such as abstract coupling and layering to promote loosely coupled systems.
Design patterns: Abstract Factory , Bridge , Chain of Responsibility , Command , Facade , Mediator , Observer .
7. Extending functionality by subclassing. Customizing an object by subclassing often isn't easy. Every new class has a fixed implementation overhead (initialization, finalization, etc.). Defining a subclass also requires an in-depth understanding of the parent class. For example, overriding one operation might require overriding another. An overridden operation might be required to call an inherited operation. And subclassing can lead to an explosion of classes, because you might have to introduce many new subclasses for even a simple extension.
Object composition in general and delegation in particular provide flexible alternatives to inheritance for combining behavior. New functionality can be added to an application by composing existing objects in new ways rather than by defining new subclasses of existing classes. On the other hand, heavy use of object composition can make designs harder to understand. Many design patterns produce designs in which you can introduce customized functionality just by defining one subclass and composing its instances with existing ones.
Design patterns: Bridge , Chain of Responsibility , Composite , Decorator , Observer , Strategy .
8. Inability to alter classes conveniently. Sometimes you have to modify a class that can't be modified conveniently. Perhaps you need the source code and don't have it (as may be the case with a commercial class library). Or maybe any change would require modifying lots of existing subclasses. Design patterns offer ways to modify classes in such circumstances.
Design patterns: Adapter , Decorator , Visitor .
Common causes of redesign |
Design patterns |
Abbr. |
1. Creating an object by specifying a class explicitly. |
Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. |
FAP |
2. Dependence on specific operations. |
Chain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Command: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. |
CC |
3. Dependence on hardware and software platform. |
Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Bridge: Decouple an abstraction from its implementation so that the two can vary independently. |
BA |
4. Dependence on object representations or implementations. |
Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Bridge: Decouple an abstraction from its implementation so that the two can vary independently. Memento: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. Proxy: Provide a surrogate or placeholder for another object to control access to it. |
BAPM |
5. Algorithmic dependencies. |
Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations. Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Visitor: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. |
BITVS |
6. Tight coupling. |
Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Bridge: Decouple an abstraction from its implementation so that the two can vary independently. Chain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Command: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Façade: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Mediator: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. |
BACCOFM |
7. Extending functionality by subclassing. |
Bridge: Decouple an abstraction from its implementation so that the two can vary independently. Chain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Composite: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Decorator: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. |
BCCDOS |
8. Inability to alter classes conveniently. |
Adapter: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Decorator: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Visitor: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. |
ADV |