What is Design Pattern?
It is a set of software solutions acquired from the works, trials, errors and experiences of many developers to obtain an optimal and reusable code by other programmers.
Benefits and importance of Design Pattern :
1. save time and resources
Of course, design patterns are ready-made solutions and a clear approach with enough content to understand and assimilate. Therefore, perhaps the most important reason to learn design patterns is that patterns facilitate the design and support of programs
Learning them will save you a lot of time and may reduce the costs of the program you are working on. Taking into account that these solutions are effective solutions if you use them wisely, which sometimes gives your program much better performance and this, of course, saves the resources of the device on which you are working.
2-easy adjustment
The biggest problem you may face in your programming life is code modification, as companies always hire new programmers, and often you will need to understand the code and then modify it. And understanding the code is not at all easy, even if the code is familiar. Design patterns provide well-known solutions among programmers , as soon as he sees a class in the SINGLETON style, he will immediately know the responsibility of this class because he previously used the same method in other programs.
3-tried and tested solutions
You will spend less time by applying ready-made solutions than by reinventing the wheel.
4-standardization of the code
You will have fewer problems since you are using typical unified solutions whose hidden problems have been discovered and solved.
5-dictionary for the world of programmers
You will only need to mention the name of the design style you used when you want to communicate the design idea to other programmers, instead of spending an hour explaining the new design you created and listing the required categories.
Tow types of Designe Patternn :
1 Adapter
It means an intermediary that comes between two incompatible things to connect them, in other words, as if you are installing something on a second thing to connect them, and the simplest example is chargers of different types (Square and round) do not all fit into the wall socket, so we need an"Adapter" connecting them.
It is also the case in the Adapter Design Pattern so that it is based on the principle of creating a separate class that acts as an intermediary between the incompatible classes.
A question may come to your mind as to why we do not change incompatible classes so that they become compatible.
The answer lies in the principle of Open Closed Principle
As a simplified example, suppose we want to illustrate the family (felines ) programmatically, but in the application we encountered a problem, so that all members of this family of beings (lions, tigers,... Cats, they do not roar, but they meow, we cannot make cats direct sons of felines, but we need an intermediary who links the Meow process of the cat with the roar process of the main species, so we need to design another class CatAdapter, for example, to do this, and we can then consider it a direct son of felines.
2 Composite Design Patterns
A composite pattern is a structural design that allows a set of objects to be mounted in tree structures that are treated in the same way on the basis that they are one instance of the same type of object.
In short, the composite Pattern is used only in one case, which is when the model on which your application is based can be represented in a tree form, for example, you have two types of Objects, products and boxes, so that the box can accommodate products inside and other boxes are also smaller than it in size, and those new boxes can contain other boxes inside, and so on until the number of existing boxes is complete.
In another example, suppose we want to model a system that demonstrates the installation of a car in a programmatic way, our initial thought might be to have each element forming a separate Object, so that we have a special object of the Engine type and another object of the CarBody type and also an object of another type, but this leads to chaos. Especially if we want to calculate the cost of the engine for example because it, in turn, has a bunch of other elements.
This is where the strength of the Composite Design Pattern lies so that we can better imagine these elements, it allows us to divide them into a tree diagram, from which we know the components for each part sequentially, for example, the car components are the bodywork, the engine, and then the engine components can be divided, for example, into Spark Plugs, valves, etc., as well as we can divide the car body into doors and frames.
Through the Composite pattern application, we can deal with any subtree (an element and the set of its constituent elements) separately, and get their information with ease, so that each Object contains a list of the Objects that are components of this main Object, through this structuring we can access the details of any component in addition to its subcomponents.