What are Design Patterns?

In this article I’m going to discuss what are the software design patterns, why are they useful, and what drawbacks may have from time to time. At the end I will show you how a pattern algorithm can be visually represented, by the so-called UML diagrams.

Overview

Design patterns are commonly used techniques in software architecture. As the name โ€œpatternโ€ suggests they are like reusable solutions for a variety of problems. It is important to note that they are not finished code snippets or algorithms, which can be directly used in your code. They are more like templates and provide a proven solution for a specific problem.

There are a lot of common software problems, which can be solved with design patterns. The benefit of using a specific design pattern is that you get time-proven and language-independent solutions. In other words, it was implemented, tested, and verified by millions of developers in a variety of programming languages, to be the right choice for the problem. If you are familiar with the design patterns, you can speed up the development. For instance – you donโ€™t have to think of a way to force an object to have only one instance in the application โ€“ you can just read about the Singleton design pattern, which will give you the solution. If you have objects with different behaviors and need to decide which object to use at runtime, you can use the Strategy pattern, and so on.

Types of patterns

These were only two examples of more than 20 design patterns out there, which are classified into 3 main groups:

  • Creational patterns – Like the name suggests they deal with problems related to object creation. The Singleton, mentioned above, is a creation pattern, dealing with the problem of creating only one object instance per application. Find all creational patterns full list here.
  • Structural patterns โ€“ focused on classesโ€™ and objectsโ€™ structure and composition. Helps us to assemble the objects in a way they remain flexible, efficient, and easy to extend. Find all structural patterns full list here.
  • Behavioral patterns โ€“ Focused on communication and assignment of responsibilities between objects. Find all behavioral patterns full list here.

Pros and cons

Design patterns may be useful solutions, but they also have some drawbacks. Using a pattern in a small project can lead to over-engineering, rather than simplicity. Always consider whether is it worth the effort to use a pattern, where a fast and easy solution can be applied.

Yes โ€“ design patterns can make your code easy to extend in the future, but the implementation may slow the development, which may not be required in some cases. In this scenario, you can use the fast solution and if needed in the future โ€“ refactor to a design pattern. Also, using patterns requires extensive knowledge of understanding exactly how they work.

In conclusion, here are the summarized pros and cons:

Design patterns - pros and cons
Design patterns – pros and cons

What is an UML Diagram?

Design patterns are visually represented with UML (Unified Modeling Language) diagrams. It is a graphical representation of object relations. For instance, the Strategy design pattern is represented with the following diagram:

Design patterns - strategy pattern example UML diagram
Design patterns – strategy pattern example UML diagram

The diagram simply says โ€“ There is an interface Strategy, with one Boolean method Execute. The interface HAS-A relationship to the context. Depending on that context, a concrete strategy is chosen at runtime.