If you have ever developed a project you must have come across the term design pattern. Design Pattern is a general and reusable solution to a certain type of commonly occurring problem in the world of software design. Please note that Design patterns are not rules, they are guidelines or principles to build a robust, scalable and manageable code base. It is very important for a developer to choose a good design pattern that caters his needs else his code can transform into a mess very soon. I am studying the types and use cases of design patterns and shall post here whatever I learn. This series shall use Golang to provide implementations of different design patterns. This article is just an introduction to Design Patterns and their types.
What are Design Patterns?
As already mentioned, Design patterns provide a generic and reusable solution to common problems encountered during Software Design. They are the general patterns which if followed, give a clean and efficient solution to any problem. Please note that the Design Pattern is not an algorithm. Algorithms define how to solve a problem with minimum overheads. An algorithm generally translates a real-world problem into mathematics and tells the ways to solve it. In terms of a Software Product, an algorithm tells how business logic can be achieved.
Design Patterns, on the other hand, can be thought of how cleanly and efficiently the business logic can be implemented. Design Pattern provides more focussed on how different pieces of business logic interact with each other to provide a robust and most importantly a flexible code.
Design Pattern is not a rulebook that is to be followed strictly as described. The singleton design pattern is a very simple but powerful pattern. It has great applications and is very easy to implement. Most of us have been using this without even knowing what it is. We shall be covering more patterns in the later articles of the series.ibed. They are mere guidelines/principles. Design patterns are programming language independent strategies for solving a common problem. That means a design pattern represents an idea, not a particular implementation. By using the design patterns you can make your code more flexible, reusable and maintainable.
Why Design Patterns?
Have you ever tried cooking ?? Almost any dish you want to make has a predefined recipe. You are of course not forced to follow the recipe, you can cook a certain dish in other ways too. But the predefined recipe is the most followed, time tested and maybe the efficient way to cook that certain dish. Using the recipe you are sure about the process, you save yourselves from any foreseen surprises (unforeseen conditions) and are the most comfortable ride to your dish. This analogy is not perfect as your dish need not be flexible/robust or scalable. But the idea behind using design patterns is somewhat the same.
While solving any problem you can just look at the Design Patterns at your disposal, analyze which best suits the use case and start implementing them. They are time tested techniques that have been working for a lot of projects around the world.
Let us take an example: Suppose you are implementing a project that interacts with Database. You have to interact with the Database at more than one instance in your code (let us say n instances). Now before any kind of interaction you need a connection with Database. But think carefully, do you need n different connections for the n instances you are interacting with the database? The simple answer is NO. You can easily reuse the same connection at all the n instances. Thus you need to a kind of mechanism where the connection object is created only once and used at all the n instances. This is a very trivial use case of Singleton Design Pattern (which shall be explained in a later post). First, let us look at the different types of Design Patterns.
Types of Design Patterns
- Creational Design Patterns: These are the patterns concerned with classes and objects creation and use. Formally put these patterns govern how the classes are instantiated and used. They can be further divided into Class-Creational Patterns and Object-Creational Patterns. Some design patterns falling under this category are Factory Method, Abstract Factory, Builder, Singleton, Object Pool, and Prototype.
- Structural Design Patterns: These design patterns are concerned with structuring or organizing the Classes and Objects and use them as a combination to provide a larger entity or functionality. In these kinds of patterns, the larger functionality is broken down into a set of classes and objects and then they are organized in a way such that they contribute to the larger functionality. Some of the patterns falling under this category are Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Private Class Data, and Proxy.
- Behavioural Design Patterns: Behavioral patterns are about identifying common communication patterns between objects and realize these patterns. Behavioral patterns are Chain of responsibility, Command, Interpreter, Iterator, Mediator, Memento, Null Object, Observer, State, Strategy, Template method, Visitor
[table id=2 /]
This was just the introduction to Design Concept and their types. In the next articles of this series, we will try to cover one pattern at a type and write a working code (in Golang) implementing that Design Pattern.
0 Comments