Example Design Patterns

Let’s take a look at a few of the classics.

Adapter

In the adapter pattern you use an existing class (that doesn't quite have the right interface) in your application through a new class with the interface you need. In this example, we want to use an existing Landscape class in our flight simulator application, but it doesn't have the right interface, so we create an adapter class called MountainScene:

adapter.gif

Decorator

A decorator lets you dynamically add additional responsibilities to an object. This example is taken from the Java Core API:

decorator.gif

State

The State pattern makes the behavior of an object related to its state without polluting its operations with conditional statements. It gives you the appearance that an object has changed its class.

state.gif

Composite

Use the composite pattern when you have a hierarchy of objects and you want to be able to treat groups of objects as a single object. Here is an example from the Java Core API:

composite.gif

Observer

The observer pattern has subjects and dependent observers; when a subject changes state, all of its observers are automatically notified and updated. The subject doesn't have to depend on who the observers actually are; observers can register and unregister with the subject dynamically.

observer.gif

Singleton

Ensure a class can only have one instance -- and provide a global point of access to this instance.

singleton.gif

Template Method

The template method pattern is defining the skeleton of an algorithm, leaving some steps to subclasses.

templatemethod.gif

Factory Method

The Factory Method pattern is used when you have an interface for creating an object, but you don't know (until run time) exactly which class you want to instantiate.

The two main varieties are (1) the factory method takes in a parameter indicating the kind of object to create, and (2) the creator is abstract and all of its subclasses will override a factory method to create instances of the right class. Here is how the first alternative looks.

factorymethod.gif

Abstract Factory

An abstract factory has several operations for creating particular kinds of products; subclasses of the factory create the concrete products. Use an abstract factory when you want your system to be configured with a particular family of products, and you want to be able to easily replace the family.

The classic typical example is a program which is independent of a windowing system:

TODO .image abstractfactory.gif

Strategy

The Strategy pattern lets a system choose a particular policy for implementing an operation at run time. A good example are the layout managers in the Java Core API:

strategy.gif

Memento

A memento is an encapsulation of another object's (the originator's) state that a third object (the caretaker) can hold onto and send back to the originator to support undo if necessary. Only the originator can set and retrieve the state of the memento.

TODO .image memento.gif

Command

A command object encapsulates a request. With commands you can queue or log requests, implement macro commands or transactions, support undo, etc. You can parameterize objects by actions to perfom, like menu items:

TODO .image command.gif

Proxy

A proxy is a placeholder (or surrogate) for another object. Clients access the remote object through the proxy. The proxy has the same interface as the remote object.

proxy.gif

Iterator

The Iterator pattern gives you a way to access the elements of an aggregate sequentially without exposing the underlying representation. Clients don't even have to care what kind of collection they have.

TODO .image iterator.gif

Visitor

A visitor lets you perform an operation on a object structure. The operation can be defined in one place instead of split up into separate methods for each of the classes whose instances are in the object structure. This lets you add new operations without changing the classes of the elements on which they operate.

TODO .image visitor.gif

Interpreter

The Interpreter pattern is used when you can express a problem as a sentence in some language, represented by an abstract syntax tree. There is a class for each non-terminal, and each class has an interpret() method.

TODO .image interpreter.gif

Builder

In the Builder pattern you separate the construction of a complex object from its representation so the same construction process can be used to build multiple representations. The director defines the process; you pass a particular builder object to the director. Builders make complex objects step by step.

TODO .image builder.gif

Prototype

In the Prototype pattern new objects are created by cloning existing prototype objects.

TODO .image prototype.gif

Bridge

In the Bridge pattern you decouple an abstraction from its implementation so the two can vary independently.

TODO .image bridge.gif

Facade

A facade is a nice single interface to a (possibly huge) subsystem of various classes.

TODO .image facade.gif

Mediator

A mediator is an object that defines how a set of objects interacts. It simplifies things because the set of objects can communicate through the mediator rather than having each of them refer to each other.

TODO .image mediator.gif

Chain Of Responsibility

Decouple a requestor from the object that handles the request by having the request be passed along of chain of objects until one of the objects handles it.

TODO .image chainofresponsibility.gif

Flyweight

A flyweight is essentially a shared object. Use them when you have a very large number of ("fine grained") objects and storing all of them would be inefficient.

TODO .image flyweight.gif