Pattern Principles
• Encapsulate the parts that vary
• Program to an interface
Instead of using
Dog d = new Dog();
d.bark();
use
a = getAnimal();
a.makeSound();
• Use Delegation in place of inheritance
Class Duck {
FlyBehavior flyBehavior
QuackBehavior quackBehavior
}
• Open for extension, close for modification
• Convention Over Configuration
Examples:
Hibernate: if property name in XML is the same as database table column name, there is no need for column attributes.
Pattern Catalogue
singleton
adapter
bridge
composite
decorator
proxy
command
chain of responsibility
iterator
observer
template
Creation Patterns
1. Abstract Factory
Isolate creation of objects from construction code by way of using a separate concrete factory. Different concrete factory can create objects having the same interface but different behaviors.
Examples: factory creating trades of different entities
In the early days of Windows, there were options provided by MFC to create the classic 2-D Windows elements or the newer, more modern 3-D Windows elements with drop shadows. The framework creates the windows and controls hierarchy, while the operating system itself renders the control. To enable the use of 3-D elements, one only has to call one function in the InitInstance function of the application object and the CreateWindow functions subsequently would invoke the appropriate factory to create the objects.
EJBHome Interface
2. Builder
Using a separate builder class to create objects. The director class has control over the building process but the builder class does the actual building.
Examples:
CWinApp controls the creation of an application, whereas the subclasses control the actual appearance of the application. CWinApp has no idea what the resulted application would look like even though the creation process is always the same.
3. Factory Method
Create object on demand since object class is not known when container of object is created. Usually subclass is needed.
Examples: template subclasses; createDocument method in subclass
MFC CSingleDocTemplate and CMultiDocTemplate. The framework does not know what type of document you would be opening when you select the open file function, but the template would route the creation of the document to the correct document subclass that would handle the responsibility. The framework has no knowledge of what type of document is to be created, that knowledge is encoded in the template and the subclasses that you provide.
EJBHome Interface
4. Prototype
Create object by cloning an existing object. Object class must have clone method.
Examples: creating cancel ticket by cloning original ticket in Aurora Adapter
5. Singleton
Enforce only one instance of object.
Examples: Java Calendar class
Structural Patterns
6. Adapter
Examples: Aurora-to-DTE Adapter, Barracuda-to-DTE Adapter, MFC ODBC
7. Bridge
Separate implementation from construction
Examples: paired classes with *imp
8. Composite
Interface for nodes and leaves are identical so callers do not distinguish between the two.
Examples: Traverse any tree structure in a recursive fashion
9. Decorator
Wrapping client class and add behavior in such a way that is transparent to the calling class.
Examples: OWL Decorated Windows
EJBObject
10. Facade
Provide a simplified interface between multiple complex systems and multiple clients.
Examples: MQPubSub class and Sybase C++ class; ODBC/JDBC/RougeWave
11. Flyweight
One object can be shared by many instances. Typically used with singleton pattern.
Examples: Hibernate connection pool
12. Proxy
A smart pointer that minimizes access or creation of underlying object. A pointer to a remote object.
Examples: Reference counting smart pointer in C++
OLE. Windows does not know what type of OLE object it is because it calls your function to do the rendering.
EJB Remote Interface
Behavioral Patterns
13. Chain of Responsibility
A request if passed along a hierarchy of objects any or all of which can respond.
Examples: OWL event handler; MFC message map
MFC message map
14. Command
Turning command request into objects. Embedding receiver reference and command code.
Examples: Win32 WndProc callback function; support undo
15. Interpreter
Use mainly for compilers
16. Iterator
Uniform interface to iterate different structures regardless of underlying implementation
Examples: Java iterator class, C++ STL
17. Mediator
Central point of control over many display elements
Similar to Cdocument->UpdateAllViews, yet mediator controls which views, if any, are actually updated.
Examples: TAS trade entry screen
Keeping states of dialogue box controls in sync before updating document.
18. Memento
An object produces a state object which can be stored and returned by other object to restore the original object to a prior state.
Examples: Serializable classes
19. Observer
The name for Attach/Detach/Notify.
For all o in observers o->Update(), Model View Controller
Examples: Quotesheet
MFC Doc/View model. When a document changes, you call the UpdeteAllViews function of the document and all views would be updated. This is similar to the attach/detach/notify mechanism.
20. State
Using different state classes to handle different behaviors under different state. Parent class delegates all state changing actions to state classes. Sate classes receive parent reference to change state.
21. Strategy
Encapsulate different object behaviors in different classes that are interchangeable providing plug-in behaviors. This is different from Bridge in that all the strategy classes are known and that different strategies do not share the same class Imp names.
Examples: OWL Validator objects for data validation
OWL Validator objects for controls
22. Template Method
Template. Minimize primitive operations subclass must implement.
MFC CDocumentTemplate
23. Visitor
Inversion of control
Examples:
CWnd FromHandle, GetActiveWindow, FindWindow, WindowFromPoint
Other Patterns
Front Controller