Design patterns are the set of rules for solving problems in software development. In real world software development we face lot of problems. When many of these problems has a common pattern then we can create a pattern out of it. We can use that solution pattern to solve the similar next problem.
A common tendency of a developer is to fit a problem into established pattern. This approach is not wise. We should instead analyze the problem and if it matches with an existing pattern just use it. Sometimes we may need multiple patterns to solve one problem. Sometimes the same problem can be solved with two different patterns.
The developer conversation can also be improved with the help of design pattern in our day to day coding. Let say when a senior developer knows that a particular problem can be solved with a particular design patter. The senior member then can guide the other team members to use that design patter instead of asking other developers to write 20 different classes.
The design patterns are for better software desinging. We can get the best use of it if we try to practice design pattern in our day to day coding properly. Following are some most used desing patterns:
The singleton design pattern is the pattern when a single class is responsible for creating the only instance and make sure the availablity of the instance. The class provides way to access it’s only instance without needing to create a new instance.
In factory method without specifying the exact class, the objects are created by calling a factory method instead of calling a constructor.
The strategy pattern allows grouping related algorithms under an abstraction, which lets you then switch out one algorithm or policy for another without modifying the client. Instead of directly implementing a single algorithm, the code receives runtime instructions specifying which of the group of algorithms to run. Yay!
This pattern is a one-to-many dependency between objects so that when one object changes state, all its dependents are notified. This is typically done by calling one of their methods.
A builder pattern is used to… build objects. Crazy, right? Sometimes, the objects you need to create are complex, made up of several sub-objects or require an elaborate construction process. The exercise of creating complex types can be simplified by using the builder pattern. A composite or an aggregate object is what a builder generally builds.
This allows incompatible classes to work together by converting the interface of one class into another. Think of it as a sort of translator: when two heads of states who don’t speak a common language meet, usually an interpreter sits between the two and translates the conversation, thus enabling communication.