Design Patterns
The problem repeats itself, just as history repeat itself
Let’s discuss what design patterns are, why they are important, and how they help in designing object-oriented software.
Introduction
Design patterns are essentially classes and interfaces organized in a way such that it solves specific design problems using OOP concepts. At the same time, they are general enough to address future problems and evolving requirements.
There is a saying: "History repeats itself, so learn from history and others' experiences." Similarly, design patterns work in the same way. Experienced architects understand that solving problems from first principles every time is not always efficient. Instead, they reuse solutions that have worked well in the past. When they discover a good solution, they apply it repeatedly. These documented solutions are what we call design patterns.
When considering any design pattern, think about two key aspects: the problem statement and the trade-offs.
1.Problem Statement: This defines which pattern to apply. It explains the context and outlines the conditions that must be considered before applying the pattern.
2.Trade-offs: These are the consequences and results of using the pattern. It is essential to critically evaluate other design alternatives and understand the costs and benefits of applying the chosen pattern.
We can segregate the design patterns into three categories.
1.Creational: These patterns involves in the process of object creation.
2.Structural: These patterns deals with the composition of classes and object 3.Behavioral: These patterns describe how classes and objects communicate and share responsibilities.
When designing an application, first identify the important objects that the program will work with. you group the similar objects into categories called classes, class defines what those objects can do. These classes should be organized in such way that it allows to share common features or characteristics or be part of others feature, which is known as inheritance
Example: Lets say we have a class called Vehicle. It can have features like wheels, engines etc. Now, Car and bike are also vehicles but they have their own features. So you can create more specific class called Car and Bike. which are also part of Vehicle class because they both share similar features (they both have wheels and engines) but car has 4 wheels and bike has 2 wheels. So, the Vehicle class is like the parent. Car and Bike are its children.
Summary
Design patterns solve many of the day-to-day problems in object-oriented applications, and in many different ways.
You’ll see how in my next few blogs of different design patterns.
Stay tuned for more on design patterns in my next blogs. 😊