If you think about how hamburgers are made, you usually start with the bun as a base and then the lettuce, patty, cheese, vegetables and then end with another bun on top. Why is that so? It’s because it’s a design solution that allow you to hold the burger without getting grease and other messy stuff all over your hands. Just like in real life many common problems in software design can be solved by standard design solutions. in Object-oriented software design these solutions are commonly known as design patterns.
What are design patterns?
Design patterns are defined as standardized solutions for solving commonly recurring problems in a software design. They are often confused with algorithms because they both solve typical recurring problems, but they’re actually different! Algorithms implements a set of clear instructions on how to solve a problem or archive a goal. Design patterns on the other hand consist of a more abstract description of the solutions. They are used more as a general guideline or a blueprint for how to go about writing and organizing code.
Design patterns is seen as a best-practice in software design. They are very useful as they allow the developers to avoid having to ”re-invent the wheel” for general problems that other people already solved. Design patterns can also act a common language that team members can use to communicate more efficiently. Instead of having to explain explicitly every idea or suggestion for a software design, you can simply refer to an existing design patterns and say for example “Oh, just use a Singleton for that,” which saves a lot of time and avoids misunderstanding.
So what does a design pattern consist of?
an Intent of the pattern which describes briefly the problem and the solution.
a Motivation that further explains the problem and how the pattern can solve it.
a Structure of classes which shows each part of the pattern and how they are related.
a Code example in one of the popular programming languages which makes it easier to grasp the idea behind the pattern.
There are design patterns that are described with other sections than the ones listed above. But in general the majority of the design patterns are described with the steps above. In my opinion this makes it easier to compare the patterns and understand them.
How are the patterns categorized?
Acording to this guide, design patterns can be categorized by their intent into the following main categories:
– Creational patterns which provide a object creation mechanisms that increase flexibility and reuse of existing code.
Example: Singleton patterns which is used to ensure that ”a class only has one instance, while providing a global acess point to this instance”
– Structural patterns explain how to assemble objects and classes into larger structures, while keeping the structures flexible and efficient.
Example: Composite that lets you compose objects into tree structures, and then use the structures as induvidual objects.
– Behavioral patterns take care of effective communication and the assignment of responsibilities between objects.
Example: Chain of Responsibility pattern in which you pass request along a chain of handlers, where each handler can decide to eighter process the request or to pass it to the next handler in the chain.
According to this site a common critique towards design patterns is that it is seen as a dogma by novice programmers that just learned about the patterns. This means that they try to apply them in almost every situations possible, including situations where a simple piece of code would be more efficient.
I personally think that some design patterns is very beneficial if you really learn the ideas behind them. They can be used as i mentioned above as a great way of communicating design ideas and suggestions without having to take time and explicitly explain them. Although I think it’s important to note that the design patterns aren’t a one size fits all solution They are general solutions, and most software designs will have their unique problems which means that you need to be able to understand which problems can be solved by design patters and which ones can’t.
If you want to continue reading about design patterns i highly recommend that you visit this site.