What is a Design Pattern?

Design patterns are typical solutions to commonly occurring problems in object-oriented software design.

  • They are like pre-made blueprints, which can be customized to address a recurring problem within a software code.
  • They were adopted and tested by numerous projects, over a period of time and considered working.
  • They are programming language independent strategies, as they represent an idea of the implementation and not the implementation itself.
  • Most programming languages (like Java) follow these design patterns internally as they make the code more flexible, reusable, and maintainable.
  • They represent the best practices followed by an experienced object-oriented software developer.

So, knowing about design patterns helps software professionals (both experienced and beginners) in providing the best possible design solutions.

In this tutorial, we will go through everything you need to know about design patterns in software development.

What does a design pattern consist of?

Most patterns are described very formally so people can reproduce them in many contexts. Here are the sections that are usually present in a pattern description:

  • Intent of the pattern briefly describes both the problem and the solution.
  • Motivation further explains the problem and the solution the pattern makes possible.
  • Structure of classes shows each part of the pattern and how they are related.
  • Code example in one of the popular programming languages makes it easier to grasp the idea behind the pattern.

Some pattern catalogs list other useful details, such as the applicability of the pattern, implementation steps, and relations with other patterns.

Why should we learn Design Patterns?

We might manage to work as a programmer without knowing any of the design patterns for many years, a lot of people do that. We might be implementing some of the patterns without even knowing that they are design patterns. So why would you spend time learning them?

  • Design patterns are a toolkit of tried and tested solutions to common problems in software design. Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design.

  • Design patterns define a common language that you and your teammates can use to communicate more efficiently. You can say, “Oh, just use a Singleton for that,” and everyone will understand the idea behind your suggestion. No need to explain what a singleton is if you know the pattern and its name.

When should we use Design Patterns?

We must use the design patterns during the analysis and requirement phases of the Software Development Life Cycle (simply SDLC).

Having an understanding of design patterns makes the analysis easy and useful.

Criticisms

Though there are several benefits of knowing and practicing design patterns in software development, there are arguments around using them as mentioned below.

  • Unjustified Use
  • Ineffective Solutions
  • Kludges for weak programming languages

For more details about the design patterns criticisms, check this link.

Classification

Design patterns differ by their complexity, level of detail, and scale of applicability to the entire system being designed.

  • The most basic and low-level patterns are often called idioms. They usually apply only to a single programming language.
  • The most universal and high-level patterns are architectural patterns. Developers can implement these patterns in virtually any language. Unlike other patterns, they can be used to design the architecture of an entire application.

In addition, all the patterns can be categorized by their purpose as mentioned below.

Type Description Patterns
Creational Patterns Creational design patterns are concerned with the object creation mechanisms, which increase flexibility and reuse of existing code. Factory, Abstract Factory, Singleton, Prototype, and Builder
Structural Patterns Structural design patterns are concerned with how to assemble objects and classes into larger structures, keeping the structures flexible and efficient. Adapter, Bridge, Composite, Prototype, Decorator, Facade, Flyweight, Proxy
Behavioral Patterns Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects. Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor

Overall

We now know the basics of design patterns and we will go through all the design patterns individually in the further chapters.

Related Links