Design Patterns
by Prathyusha Gandra
Each pattern describes a problem which occurs over and
over again in our environment, and then describes the core of the solution to
that problem, in such a way that you can use this solution a million times
over, without ever doing it the same way twice
C. Alexander, “The Timeless Way of Building”, 1979
Patterns help you learn from other’s successes, instead
of your own failures
Mark Johnson (cited by B. Eckel)
IF
you find yourself in CONTEXT
for example EXAMPLES,
with PROBLEM,
entailing FORCES
THEN for some REASONS,
apply DESIGN FORM AND/OR RULE
to construct SOLUTION
leading to NEW CONTEXT and
OTHER PATTERNS
The term design pattern can be somewhat off-putting when you
first encounter it. But, design-pattern is just a convenient way of reusing
object –oriented code between projects and between programmers
· Creational Patterns
o Deal
with initializing and configuring classes and objects.
o
How am I going to create my objects?
· Structural Patterns
o Deal
with decoupling the interface and implementation of classes and objects.
o
How classes and objects are composed to build
larger structures.
· Behavioral Patterns
o Deal with dynamic interactions among societies of classes and objects.
o How
to manage complex control flows (communications).
Scope |
|
Purpose |
||
Creational |
Structural |
Behavioral |
||
Class |
Factory Method |
Adapter |
Interpreter |
|
Object |
· Abstract Factory · Builder · Prototype · Singleton |
· Adapter · Bridge · Composite · Decorator · Façade · Flyweight · Proxy |
· Chain of Responsibility · Command · Iterator · Mediator · Observer · State · Strategy · Visitor |
Public class PrintSpooler {
Private static PrintSpooler spooler;
Private PrintSpooler() {
}
Public static synchronized printSpooler get Spooler {
if (spooler ==
null)
spooler =
new PrintSpooler();
return
spooler;
}
}
Provides a simple decision-making class that returns one of
several possible subclasses of an abstract base class, depending on the data
that are provided
It’s used in Mathematical computations
Abstract Factory Pattern: Provides an Interface to create and return one of several families of related objects.
Builder Pattern: separates the construction of a complex object from its representation so that several different representations can be created, depending on the needs of the program.
Example: Address Book containing the group addresses and personal addresses
Prototype
Pattern: starts with an instantiated class, which it copies or
clones to make a new instance. These instances can then be further tailored
using their public methods.
Adapter Pattern: can be used to make one class interface match another for easier programming.
Composite Pattern: Creates an object (a composition of objects), each of which may be a simple or a composite object.
Proxy
Pattern: creates a simple object that takes the place of a more
complex object which may be invoked later, such as when the program runs in a
networked environment.
Flyweight
Pattern: for sharing objects, where each instance does not
contain its own state, but stores it externally. This approach allows efficient
sharing of objects to save space when there are many instances, but only a few
different types
Façade Pattern: used to make a single class represent an entire subsystem.
Bridge pattern: separates an object interface from its implementation.
Decorator
pattern: lets you add responsibilities to objects dynamically.
Chain of Responsibility pattern: allows decoupling between objects by passing a request from one object to the next in a chain until the request is recognized.
Command pattern: utilizes simple objects to represent the execution of software commands and allows you to support logging and undoable operations.
Interpreter
pattern: provides a definition of how to include language
elements in a program.
Iterator Pattern: formalizes the way we move through a list of data within a class.
Mediator Pattern: defines how communication between objects can be simplified by using a separate object to keep all objects from having to know about each other.
Observer pattern: defines how multiple objects can be notified of a change.
State pattern: allows an object to modify its behavior when its internal state changes.
Strategy Pattern: encapsulates an algorithm inside of a class.
Template Method pattern: provides an abstract definition of an algorithm.
Visitor
pattern: adds polymorphic functions to a class noninvasively.