Example Design Patterns

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:

Decorator

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

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.

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:

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.

Singleton

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

Template Method

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

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.

Image createImage(String extension) < if (extension.equals("gif") < return new GIFImage(); >else if (extension.equals("jpg") < return new JPEGImage(); >>

could be hard to extend.

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:

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:

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.

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:

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.

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.

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.

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.

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.

Prototype

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

Bridge

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

Facade

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

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.

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.

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.