Design patterns

From CSSEMediaWiki
Jump to: navigation, search

In the 1970s the architect Christopher Alexander composed a collection of Design Patterns. He wanted to include the residents of certain structures into the process of designing their buildings. Christopher Alexander describes the concept of design patterns by saying "Each pattern describes a problem which occurs over and over again in our environment, and the 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." (from [1])

In 1987 based on Alexander's ideas Kent Beck and Ward Cunningham developed design patterns for Graphical User Interfaces in Smalltalk. A year later Erich Gamma started his PhD about transfering the general idea of design patterns to software engineering. After finishing his PhD, Erich Gamma went to the States and published together with Richard Helm, Ralph Johnson and John Vlissides, also known as the Gang of Four, the book: Design Patterns - Elements of Reusable Object-Oriented Software (1995).

Peter Coad had also worked on software design patterns in parallel to the Gang of Four and published an article ([2]) about design patterns in the Communications of the ACM as early as 1992.

Design patterns are generally considered to be good solutions to common and difficult problems. Because the problems they solve are often quite complex and there may be no ideal design solutions, Design patterns break rules.

In this book they describe the following 23 Design Patterns: Design Patterns Flashcards

A page on Combining design patterns discusses how these patterns should be put together and the consequences of doing so.

An alternative practical example of pattern combining: Wal's Fish Tank.


Creational Patterns

From [3]:

"Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed and represented. A class creational pattern uses inheritance to vary the class that's instantiated, whereas an object creational pattern will delegate instantiation to another object."
  • Abstract Factory : Provide an interface for creating families of related or dependent objects without specifying their concrete classes. Object creational
  • Builder : Separate the construction of a complex object from its representation so that the same construction process can create different representations. Object creational
  • 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. Class creational
  • Prototype : Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Object creational
  • Singleton : Ensure a class only has one instance, and provide a global point of access to it. Object creational

Structural Patterns

From [3]:

"Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. ... Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality. The added flexibility of object composition comes from the ability to change composition at run-time, which is impossible with static class composition."
  • 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. Class, object structural
  • Bridge : Decouple an abstraction from its implementation so that the two can vary independently. Object structural
  • Composite : Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Object structural
  • Decorator : Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Object structural
  • Facade : 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. Object structural
  • Flyweight : Use sharing to support large numbers of fine-grained objects efficiently. Object structural
  • Proxy : Provide a surrogate or placeholder for another object to control access to it. Object structural

Behavioral Patterns

From [3]:

"Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them. ... Behavioral class patterns use inheritance to distribute behavior between classes. ... Behavioral object patterns use object composition rather than inheritance."
  • 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. Object Behavioral
  • Command : Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Object Behavioral
  • Interpreter : Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Class Behavioral
  • Iterator : Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Object Behavioral
  • 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. Object Behavioral
  • Memento : Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. Object Behavioral
  • Observer : Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Object Behavioral
  • State : Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. Object Behavioral
  • Strategy : Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Object Behavioral
  • 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. Class Behavioral
  • 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. Object Behavioral


By nature, design patterns encourage developers to copy code and ideas causing developers to repeat themselves breaking the Once and only once maxim. Some argue that this is because non dynamic languages are broken because they don't allow that level of abstraction, for example in Lisp 16 of the 23 were either invisible or much simpler than in C++ [1]. The other common argument against the use of design patterns is that many people take them as blocks of good design and use a design pattern wherever one might possibly fit rather than weighing up the pros and cons of using the design pattern in the particular context they are trying to use it in [2].


  1. ^Christopher Alexander, Sara Ishikawa, Murray Silverstein, Max Jacobsen, Ingrid Fiksdahl-King, and Shlomo Angel. A Pattern Language. Oxford University Press, New York, 1977.
  2. ^Peter Coad. Object-oriented patterns. Communications of the ACM, 35(9):152–159, 1992.
  3. ^E. Gamma, R. Helm, R. Johnson, J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, Boston, MA, 1995.

See also

Personal tools