Hey Geeks! Are you planning to pursue a career in software development? If so, design patterns are a skill that you’ll need pickup soon. Therefore we introduce this new series, where we will dedicate one article to each pattern and attempt to explain it in the easiest way possible. But before we dive in, let us first look at what design patterns really are.
What are design patterns?
Building good software requires planning and design. Just like we design the user interface of an app, we also need to carefully design how we write the business logic. For example, consider the Logger class in Java. It helps us log events/messages to a file, in order for us to later use them for debugging. Now ask yourself this question:
Is it a good design decision to create a new object of the Logger class wherever we want to log messages in our code?
The answer is no. It may feel like, does it really matter? It doesn’t, if it’s a small application with five or six classes. But in real life, apps contain many classes and thousands of lines of code. Creating a new object every time we need to log something will overload the memory unnecessarily.
The solution then is to write the Logger class in such a way that, only one object of it can be created at a time. Instead of creating a new object, we just reuse the existing one. This saves lots of memory and hence makes our app perform better.
The Solution to Common Problems
Problems like the one stated above keep cropping up in software development. That’s why, experienced developers and engineers have come up with a list of patterns, or styles of coding, that alleviate these problems.
Therefore, design patterns in programming is nothing but a set of coding styles, that act as solutions to typical problems in software development. So how many design patterns are there? Design patterns are categorized into three segments:
- Creational Patterns
- Structural Patterns
- Behaviorial Patterns
Creational design patterns are concerned with the creation of objects. These solutions help one to create objects more effectively, depending on the situation. Here’s the list of patterns which come under this category:
- Singleton Pattern
- Builder Pattern
- Prototype Pattern
- Factory Pattern
- Abstract Factory Pattern
Structural design patterns are concerned with efficient assembly of different objects. Therefore, implementing these patterns wherever appropriate can make your application more scalable. The patterns under this category are:
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
Behaviorial design patterns contain solutions that help us to correctly assign responsibilities between objects. They are concerned more about algorithms. Following are the patterns included in this category:
- Chain of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern
- Strategy Pattern
- Template Method Pattern
- Visitor Pattern
So now you have the big picture. Design Patterns is a vast subject. Mastering it takes time, patience and practice. In order to do so, you will need resources that are easy to understand.
That’s why, we are going to take each design pattern from each category listed above and dedicate one article to each. We will explain them thoroughly with easy to understand examples. Each article will be directly linked from here.
Our language of choice for this free tutorial will be Java, for its excellent object-oriented programming implementation. See you in my next article, where we take a deep dive into the Singleton pattern! 😎
While I am writing the next tutorial, why not take a look at our pick of the top five websites to practice coding?