A software design pattern describes a reusable solution to a commonly needed behavior in software. A design pattern is not a rigid structure to be copied directly into source code. Rather, it is a description of and a template for solving a particular type of problem that can be used in many different contexts, including different programming languages and computing platforms. Design patterns can be viewed as formalized best practices that the programmer may use to solve common problems when designing software.

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply mutable state may be unsuited for functional programming languages. Some patterns can be rendered unnecessary in languages that have built-in support for solving the problem they are trying to solve, and object-oriented patterns are not necessarily suitable for non-object-oriented languages.

History

Patterns originated as an architectural concept by Christopher Alexander as early as 1977 in A Pattern Language (cf. his article, "The Pattern of Streets," JOURNAL OF THE AIP, September, 1966, Vol. 32, No. 5, pp. 273–278). In 1987, Kent Beck and Ward Cunningham began experimenting with the idea of applying patterns to programming – specifically pattern languages – and presented their results at the OOPSLA conference that year. In the following years, Beck, Cunningham and others followed up on this work.

Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 <!-- 1994, not 1995. See talk page. --> by the so-called "Gang of Four" (Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides), which is frequently abbreviated as "GoF". That same year, the first Pattern Languages of Programming Conference was held, and the following year the Portland Pattern Repository was set up for documentation of design patterns.

Although design patterns have been applied practically for a long time, formalization of the concept of design patterns languished for several years.

Practice

Design patterns can speed up the development process by providing proven development paradigms. Effective software design requires considering issues that may not become apparent until later in the implementation. Freshly written code can often have hidden, subtle issues that take time to be detected – issues that sometimes can cause major problems down the road. Reusing design patterns can help to prevent such issues, and enhance code readability for those familiar with the patterns.

Software design techniques are difficult to apply to a broader range of problems. Design patterns provide general solutions, documented in a format that does not require specifics tied to a particular problem.

In 1996, Christopher Alexander was invited to give a Keynote Speech to the 1996 OOPSLA Convention. Here he reflected on how his work on Patterns in Architecture had developed and his hopes for how the Software Design community could help Architecture extend Patterns to create living structures that use generative schemes that are more like computer code.

Motif

A pattern describes a design motif, a.k.a. prototypical micro-architecture, as a set of program constituents (e.g., classes, methods...) and their relationships. A developer adapts the motif to their codebase to solve the problem described by the pattern. The resulting code has structure and organization similar to the chosen motif.

Domain-specific patterns

Efforts have also been made to codify design patterns in particular domains, including the use of existing design patterns as well as domain-specific design patterns. Examples include user interface design patterns, information visualization, secure design, "secure usability", Web design and business model design.

The annual Pattern Languages of Programming Conference proceedings include many examples of domain-specific patterns.

Object-oriented programming

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply mutable state may be unsuited for functional programming languages. Some patterns can be rendered unnecessary in languages that have built-in support for solving the problem they are trying to solve, and object-oriented patterns are not necessarily suitable for non-object-oriented languages.

Examples

Design patterns can be organized into groups based on what kind of problem they solve.

Creational

A creational pattern creates objects.

{| class="wikitable"

|-

! Name

! Description

! In Design Patterns

! In Code Complete

! Other

|-

| Abstract factory

| Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

|

|

|

|-

| Builder

| Separate the construction of a complex object from its representation, allowing the same construction process to create various representations.

|

|

|

|-

| Dependency Injection

| A class accepts the objects it requires from an injector instead of creating the objects directly.

|

|

|

|-

| Factory method

| Define an interface for creating a single object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

|

|

|

|-

| Lazy initialization

| Tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. This pattern appears in the GoF catalog as "virtual proxy", an implementation strategy for the Proxy pattern.

|

|

|

|-

| Multiton

| Ensure a class has only named instances, and provide a global point of access to them.

|

|

|

|-

| Object pool

| Avoid expensive acquisition and release of resources by recycling objects that are no longer in use. Can be considered a generalisation of connection pool and thread pool patterns.

|

|

|

|-

| Prototype

| Specify the kinds of objects to create using a prototypical instance, and create new objects from the 'skeleton' of an existing object, thus boosting performance and keeping memory footprints to a minimum.

|

|

|

|-

| Resource acquisition is initialization (RAII)

| Ensure that resources are properly released by tying them to the lifespan of suitable objects.

|

|

|

|-

| Singleton

| Ensure a class has only one instance, and provide a global point of access to it.

|

|

|

|}

Structural

A structural pattern organizes classes and objects to form larger structures that provide new functionality.

{| class="wikitable"

|-

! Name

! Description

! In Design Patterns

! In Code Complete

|-

| Marker

| Empty interface to associate metadata with a class.

|

|

|

|-

| Module

| Group several related elements, such as classes, singletons, methods, globally used, into a single conceptual entity.

|

|

|

|-

| Proxy

| Provide a surrogate or placeholder for another object to control access to it.

|

|

|

|-

| Twin

| Twin allows modeling of multiple inheritance in programming languages that do not support this feature.

|

|

|

|}

Behavioral

A behavioral pattern describes collaboration between objects.

{| class="wikitable"

|-

! Name

! Description

! In Design Patterns

! In Code Complete

! Other

|-

| Active Object

| Decouples method execution from method invocation that reside in their own thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.

|

|

|-

| Balking

| Only execute an action on an object when the object is in a particular state.

|

|

|-

| Binding properties

| Combining multiple observers to force properties in different objects to be synchronized or coordinated in some way.

|

|

|-

| Compute kernel

| The same calculation many times in parallel, differing by integer parameters used with non-branching pointer math into shared arrays, such as GPU-optimized Matrix multiplication or Convolutional neural network.

|

|

|-

| Double-checked locking

| Reduce the overhead of acquiring a lock by first testing the locking criterion (the 'lock hint') in an unsafe manner; only if that succeeds does the actual locking logic proceed.

Can be unsafe when implemented in some language/hardware combinations. It can therefore sometimes be considered an anti-pattern.

|

|

|-

| Event-based asynchronous

| Addresses problems with the asynchronous pattern that occur in multithreaded programs.

|

|

|-

| Guarded suspension

| Manages operations that require both a lock to be acquired and a precondition to be satisfied before the operation can be executed.

|

|

|-

| Join

| Join-pattern provides a way to write concurrent, parallel and distributed programs by message passing. Compared to the use of threads and locks, this is a high-level programming model.

|

|

|-

| Lock

| One thread puts a "lock" on a resource, preventing other threads from accessing or modifying it.

|

|