Software Design Patterns
By: Stellar Software Company
Tags: Gang Of Four, Software Architecture, Software Design Patterns, Design Patterns, Programming

What are Software Design patterns?

Wikipedia says following about the Software Design Patterns -

A Software Design Pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. Rather, it is a description or template for how to solve a problem that can be used in many different situations. Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.

Design patterns are templates to solve common problems in Software engineering. They provide repeatable solutions, which you can apply to some of the common problems you come across.

Design patterns are not finished solution or a code or a class or a library that you can import in your project. They are a generic solution for solving a problem. Each project will have a slightly different way to solve it.

Why should you learn Design patterns?

Source Google/Giphy

Why would you even consider learning Design patterns? You can write codes even without them. I can give you a few reasons why -

Something you need to know to be a better Software Engineer — Design patterns can help you be a better Software Engineer. In computer programs, very often some of the complex problems can be solved easily and elegantly by the use of Design patterns. Just by knowing about the Design patterns and knowing how to apply one, you can save a lot of time and improve the overall quality of code you deliver. All of this will help us grow and become a better Software engineer.

— Design patterns can help you be a better Software Engineer. In computer programs, very often some of the complex problems can be solved easily and elegantly by the use of Design patterns. Just by knowing about the Design patterns and knowing how to apply one, you can save a lot of time and improve the overall quality of code you deliver. All of this will help us grow and become a better Software engineer. Solutions already exist — Most of the problems you might encounter in your life, writing codes are not new. There has already been a bunch of people who have seen that problem, worked on it for days and came up with a solution and most likely written about it somewhere. You can use their knowledge and experience. You do not need to build the solution from scratch. You only have to learn to identify them.

— Most of the problems you might encounter in your life, writing codes are not new. There has already been a bunch of people who have seen that problem, worked on it for days and came up with a solution and most likely written about it somewhere. You can use their knowledge and experience. You do not need to build the solution from scratch. You only have to learn to identify them. Improve communication of idea using common vocabulary — You can easily convey your idea by just using the Design pattern name instead of describing all the technical details. It helps you in communicating the idea clearly.

— You can easily convey your idea by just using the Design pattern name instead of describing all the technical details. It helps you in communicating the idea clearly. The standard way to solve a problem — Design patterns provide a tried and tested way to solve a problem. Using these tried and tested approach to solve a problem will speed up your overall development process. Using a design pattern in your project will also help you uncover issues which you might not have uncovered until very late had you not used it.

— Design patterns provide a tried and tested way to solve a problem. Using these tried and tested approach to solve a problem will speed up your overall development process. Using a design pattern in your project will also help you uncover issues which you might not have uncovered until very late had you not used it. Code readability — Design patterns also improves code readability amongst the people familiar with the Design patterns.

The Gang of Four

A group of four very smart people wrote a book title — Design Patterns: Elements of Reusable Object-Oriented Software back in 1994. These four people were Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. They are also referred to as GoF.

Their book has been a cornerstone in the field of Software Engineering. Published over 25 years ago, this book remains very popular and a must-read amongst Software Engineers. It documented 23 design patterns in three categories, namely Creational, Structural & Behavioral.

Types of Design Patterns

The Gang of Four categorized their 23 Design Patterns in three groups, namely -

Creational — They deal with the creation of objects. Singleton & Factory are examples of creational design patterns.

— They deal with the creation of objects. Singleton & Factory are examples of creational design patterns. Structural — They deal with the relationship between the objects. Adapter is an example of structural design patterns.

— They deal with the relationship between the objects. Adapter is an example of structural design patterns. Behavioral — They deal with interaction & communication between the objects. Strategy is an example of behavioral design patterns.

Commonly used Design Patterns

Singleton Pattern — There can only be one instance of a given class. This is one of the most used/common design patterns. Singleton solves the problem of ensuring only one instance of a class. This pattern is generally used with other patterns like Facade, Factory & etc because we want only one instance of those classes. You can find details about this design pattern here.

— There can only be one instance of a given class. This is one of the most used/common design patterns. Singleton solves the problem of ensuring only one instance of a class. This pattern is generally used with other patterns like Facade, Factory & etc because we want only one instance of those classes. You can find details about this design pattern here. Strategy Pattern — This design pattern allows you to select algorithm/logic at runtime. For example, your program needs to pick the kind of Sorting algorithm it needs to use based on some logic.

— This design pattern allows you to select algorithm/logic at runtime. For example, your program needs to pick the kind of Sorting algorithm it needs to use based on some logic. Adapter Pattern — This design pattern works to connect two incompatible interfaces, that cannot be connected. It wraps an existing class in a new interface to make it compatible with client interface.

— This design pattern works to connect two incompatible interfaces, that cannot be connected. It wraps an existing class in a new interface to make it compatible with client interface. Factory Pattern — This pattern allows the creation of objects without having to specify the exact class of the object.

— This pattern allows the creation of objects without having to specify the exact class of the object. Proxy Pattern — In the Proxy pattern, a class functions as an interface to ‘something’. This ‘something’ could be a network request to get JSON response from a REST API or could be getting the JSON response from the locally cached Database.

These are some of the commonly used ones. GoF came up with 23 design patterns in total in their book. Following is the complete list as categorized by them. I plan to write a detailed blog on each one of them.

GoF’s 23 design patterns in 3 categories

Our approach

We are going to cover each of the Design patterns in details with multiple examples. From concept to actual code. I will also make sure I share the link of the git repo which you can refer, re-use & share.

Even though we are going to discuss all the 23 design patterns, you do not need to be an expert in all of them. You need to know a handful of the important ones and have a basic understanding of the rest.

Programming Language

In the more detailed discussion, I will try to use more than one language for the examples. Java will be one of them, the second language can be C++ or Swift or both.

Anti-Patterns

It is prudent for us to cover the Anti-Patterns at this point. We will discuss this in detail in upcoming blogs.

An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.

As the name might suggest Anti-Patterns are common solutions to commonly occurring problems which give bad results. These Anti-Pattern solutions may appear effective and appropriate in the beginning but fail to give good results. They have more bad consequences than good ones.

Anti-patterns are some patterns in Software Engineering that are considered as bad programming practices. Contrary to design patterns which are common ways to solve common problems which have been formalized and are generally considered a good development practice, anti-patterns are the opposite and are undesirable.

For every Anti-Patterns there generally is an effective, documented & proven solution. Anti-Patterns are often result of insufficient knowledge. These Anti-Patterns have been used in real-world projects and found to be ineffective.

There must be at least two key elements present to formally distinguish an actual anti-pattern from a simple bad habit, bad practice, or bad idea: A commonly used process, structure, or pattern of action that despite initially appearing to be an appropriate and effective response to a problem, has more bad consequences than good ones. Another solution exists that is documented, repeatable, and proven to be effective.

A common example of Anti-Pattern is God object or Kitchen sink class. Having one class to do a number of things.

Path forward

The book written by The Gang of four titled Design Patterns: Elements of Reusable Object-Oriented Software is the book to read. Additionally, you can also read — Head First Design Patterns by Eric & Elisabeth Freeman. My favorite is the Head First book. Head First have so many books in their collection and I adore their style. You should check it out here.

I plan to write detailed post on each one of the Design patterns. Continue reading them here.