Design Patterns, a software engineer’s handbook



Author: Kostas Papanikolaou

Categories: Design

Design Patterns, a software engineer’s handbook

Have you ever seen or heard the initialism “RTFM”? It is one of the most popular initialisms in the world, and especially the digital world. It is an acronym for the phrase “read the [freaking] manual”, and has been around for quite some time. Everything complex needs a manual, and common problem-solving in programming is no exception. Enter, Design Patterns.

On October 21st, 1994, a team of software engineers called “Gang of Four” (GoF), comprised of Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, published the book “Design Patterns: Elements of Reusable Object-Oriented Software”. It describes software design patterns and is divided into two parts, one exploring the pros and cons of OOP (object-oriented programming), and one describing 23 classic design patterns, with examples in C++ and Smalltalk.

Throughout the decades, the four authors of the book have been referred to as the “Gang of Four” and are considered pioneers in the world of software engineering, thanks to design patterns

Find out more coding books in our blog, “Coding is vital, reading is fundamental

What are Design Patterns?

There is no simpler way to explain what design patterns are to someone who has no experience in the world of software engineering, than the following:

Design patterns are reusable solutions to problems that commonly occur during programming, and come in the form of descriptions or templates on how to solve a problem that can be used in different situations

Whether you are a seasoned or junior software engineer, or just someone who is tech-savvy, you know that programming includes -among other things- a lot of testing and troubleshooting. It is one of the most complex professions in the world when it comes to problem-solving and finding bypasses or fixes to issues that occur more often than not.

This fact about programming is why the authors of “Design Patterns: Elements of Reusable Object-Oriented Software” were eventually called “Gang of Four”, gaining the respect of their peers and setting in “stone” a manual which allows software engineers everywhere -regardless of the era- get their hands-on solutions to common problems that occur when creating an application or system.

Object-oriented design patterns usually show relationships and interactions between classes or objects, without the need to specify the final application classes or objects involved. They are templates of solutions that apply in many situations.

Design Pattern Classifications

Initially, the “bible” of problem-solving in programming categorized design patterns into 3 sub-classifications, based on the kind of issue they can solve:

  • Creational
  • Structural
  • Behavioral

A year after the initial release of “Design Patterns: Elements of Reusable Object-Oriented Software”, R. Greg Lavender and Douglas C. Schmidt published the paper “Active Object – An Object Behavioral Pattern for Concurrent Programming”, introducing the term “Concurrency Pattern”, a fourth classification of design patterns. An earlier version of this paper appeared in the book “Pattern Languages of Program Design 2”, edited by Jim Coplien, Norm Kerth, and one of the “Gang of Four”, John Vlissides.

Each classification of design patterns deals with a different type of situation that commonly appears in the world of programming. They help solve different types of issues that arise quite often in the work of software engineers and the categorization helps programmers put design patterns into perspective, and easily understand them.

Creational patterns

Creational patterns are design patterns that help software engineers deal with object creation mechanisms. By controlling the object creation in many different ways, creational patterns help software engineers solve design problems or added complexity issues when object creation happens. Among the most well-known creational design patterns, software engineers will find the abstract factory pattern, builder pattern, factory method pattern, prototype pattern, and singleton pattern. The latter ensures that a class has only one instance, while also providing a global point that allows access to that instance.

Structural patterns

Structural patterns are design patterns that identify a simple way to realize the relationships among the entities. Commonly used structural patterns include the adapter pattern, bridge pattern, composite pattern, façade pattern, and marker pattern. The adapter pattern allows for the “adaptation” of one interface for a class into one that a client expects, while the marker pattern is an empty interface that allows associating metadata with a class.

Behavioral patterns

Behavioral patterns are design patterns that help software engineers identify communication patterns among objects, increasing flexibility in carrying out communication. Examples of behavioral patterns include the command pattern, mediator pattern, protocol stack, specification pattern, and visitor pattern. The latter is a pattern used to separate an object from an algorithm, while the protocol stack allows communications to be handled by multiple layers which create an encapsulation hierarchy.

Concurrency patterns

Last but not least, concurrency patterns are design patterns that allow software engineers to deal with the multi-threaded programming paradigm. The most popular concurrency pattern is Active Object, the one presented in the paper “Active Object – An Object Behavioral Pattern for Concurrent Programming” by Lavender and Schmidt. Active Object is a design pattern that decouples method execution from method invocation for objects that reside in their own thread of control. Active Object consists of six different elements: a proxy, an interface, a list of pending requests from clients, a scheduler, the implementation of the active object method, and a callback or variable.

Do Design Patterns have cons?

Roughly put, everything in programming can have cons, and it is practically impossible to find “the perfect application/system”. In our Software Testing blog, we have discussed why the aforementioned fact about programming always having flaws makes software testing so important. When it comes to design patterns, they have received criticism throughout the years, the primary one being that they are simply workarounds for missing features in the object-oriented language C++.

When Erich Gamma, one of the “Gang of Four”, was asked about a possible revisit to the book in 2009, he said that the four authors discussed this possibility in 2005, concluding they would have recategorized some patterns and would have added a few more. In addition, Gamma stated in that interview with InformIT that he wanted to remove the Singleton creational pattern, but no consensus was found among the authors.

What is OOP?

Understanding design patterns requires understanding Object-oriented programming (OOP), since design patterns were created to solve common problems that occur within OOP. OOP is a programming paradigm that is based on the concept of “objects”. Said objects can contain code and data. Code in OOP is found in the form of procedures, known also as “methods”, while data is found in the form of fields, also known as “attributes” or “properties”.

Object-oriented languages that widely used and are among the most popular in the world include:

  • PHP
  • Python
  • C++
  • Java
  • JavaScript
  • Visual Basic.NET

Tags: design patterns, object-oriented languages, object-oriented programming, programming, software engineering