
- Clean Code - Home
- Clean Code - Best Practices & Principles
- Clean Code - Naming Conventions
- Clean Code - Comments & Documentation
- Clean Code - Testing Practice
- Clean Code - SOLID Principles
- Clean Code - Design Patterns
- Clean Code - Code Smells
- Clean Code - Refractoring
- Clean Code Useful Resources
- Clean Code - Useful Resources
- Discuss Clean Code
Clean Code - Design patterns
Design patterns are templates that we can follow while writing code. Those are reusable ways to solve coding problems. This isn't a solution that is directly written, it's more like a guide to help us structure your code in a clear and manageable way.
Types of Design Patterns
There are three main types of design patterns −
Creation Design Pattern: These design patterns are all about finding the right way to create objects based on what you need. If you just create objects the way you always do, it can sometimes cause problems and face issues and also look messier. Object handling becomes easy because of these patterns, These make it easier and more suitable for what youre trying to do.
Structural Design Patterns: Structural design patterns are all about figuring out how to put objects and classes together to build something bigger. If you just try to combine things in the way you always do, The structure can confuse you sometimes and it becomes hard to manage. By showing the right way for connecting objects and classes these patterns help you. Then everything fits together and looks organized.
Behavioral Design Patterns: These patterns deal with object collaboration. They describe how objects interact to fulfill a task. Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
Common Design Patterns
There are many design patterns available, but some of the most commonly used ones are:
- Singleton Pattern: Ensures that a class has only one instance and provides a global point of access to that instance.
- Factory Pattern: This pattern helps us to create an object even without creating an exact class for the object.
- Observer Pattern: This pattern helps to create a one-to-many dependency between objects.
- Strategy Pattern: This pattern forms a group of different-different algorithms, and encapsulates each one of them as a separate class. This makes interchanging possible.
- Decorator Pattern: This pattern helps us to add more functionality or responsibilities to an object dynamically without changing its structure.
- Adapter Pattern: Objects with different interfaces can work together with the help of this pattern.
- Facade Pattern: It provides a simplified interface to a very complex subsystem. It does hide the complexity of the system.
- Template Method Pattern: This algorithm defines the structure of the algorithm in its base class but also allows sub-classes to override a specific method or step without changing its structure.
Conclusion
Design patterns are useful for everyone who writes code to make code clean and neat. The pattern gives you a simple way to fix common problems that cause when you build something. Learning these patterns definitely makes you a better coder.