> Articles > Design Patterns

Design Patterns

Design Patterns are time-tested solutions for commonly recurring problems in object oriented design.

What are Design Patterns?

Christopher Alexander, the person who came up with concept of patterns says - “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”.

Alexander was talking about patterns that appear in buildings and towns. But the same is true about the patterns that appear in object oriented design for solutions resolving the recurring problems.

"Design patterns are solutions for commonly recurring problems in object oriented design."

These are the solutions that were successfully used by experienced designers to solve their design problems. So when we use design patterns, we are reusing a solution that has worked in the past for designers. Familiarity with these patterns enables designers to apply them in their projects, thus reusing solutions instead of re-discovering them. So it becomes easier to reuse successful designs and architectures.

Design Patterns provide an effective way to understand and design software systems. These patterns are used to design reusable, flexible and elegant object oriented software. Most of the well-structured object oriented architectures make use of these patterns, and so knowledge of these patterns is very important for software developers and designers.

History/Origin of design patterns

An Architect named Christopher Alexander observed designs of several buildings and towns, and discovered that there were many common architectural aspects that were used to solve similar problems. He identified such common solutions and called them patterns.

In 1990's Alexander's idea of patterns was applied to software design and design patterns were first documented in the book - "Design patterns – Elements Of Reusable Object-Oriented Software By Eric Gamma, Richard Helm, Ralph Johnson, John Vissides". These authors are commonly known as "Gang Of Four". Authors of this book identified several patterns that were present in different software systems and named them.

Why study design patterns

• Avoid reinventing the solution.

We can benefit from the experience of other designers who have successfully applied these patterns in their designs. We get readymade high quality solutions to commonly recurring problems in design.

• Creating reusable and modifiable object oriented design.

Applying design patterns in your project will make it reusable, flexible and easily maintainable. There is no need of redesigning every time, the design has to be reused.

• Better understanding of basic object oriented principles

Knowledge of Design Patterns improves your understanding of basic object oriented principles. These patterns reinforce good object oriented principles like-

1. Favour aggregation over class inheritance
2. Program to an interface, not an implementation
3. Encapsulating the concept that varies

Design of complex systems can be improved by using these patterns and thus avoiding large inheritance hierarchies.

• Better understanding of existing systems and design

Design patterns will enhance your ability to understand existing systems and design new ones effectively.

• Common vocabulary

Design patterns provide a common vocabulary that can be used during the design phase of a project. A designer can use the common terminology provided by design patterns to avoid confusing descriptions in the design phase. When you have to explain your design, you can just tell that you have used a particular design pattern instead of explaining all the details. This way you can focus on real issues in your design rather than discussing the implementation details in the analysis and design phase.

Thus design patterns can be used for team communication and documentation, senior team members should know Design Patterns as they can use it in their design and junior members should know, so that they can understand how their seniors have designed the system.

Describing Design Patterns

The Gang Of Four came up with format for describing design pattern. Here is the format-

Design Patterns format or template

Any design pattern should be described in this format only, so it will be easy to understand for everyone.

The Catalog of Design Patterns

The Gang Of Four came up with catalog of 23 design patterns. Here they are-

Design Patterns Catalog

Classification of Design Patterns

Design patterns can be classified into groups based on two criteria. The first one is based on pattern's purpose. The second one is based on scope and specifies whether the pattern applies to classes or objects.

Design Patterns Classification

Creational Design Patterns

Creational patterns concern the process of object creation. These are the patterns comes under creational patterns.

Design Patterns - Creational

Here Factory Method pattern is Class pattern and rest are Object patterns.

Structural Design Patterns

Structural patterns are concerned with the composition of classes or objects to form larger structures. These are the patterns comes under Structural patterns.

Design Patterns - Structural

Here Adapter - Inheritance pattern is Class pattern and rest are Object patterns.

Behavioral Design Patterns

Behavioral patterns characterize the ways in which classes or objects interact and distribute responsibility. These are the patterns comes under Behavioral patterns.

Design Patterns - Behavioral

Here Interpreter and Template Method patterns are Class pattern and rest are Object patterns.

Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his course on Advanced Python.