Table of Contents
What are Design Patterns?
When the OGs were banging their heads to make their messed-up code obey S.O.L.I.D. principles, they came up with their own approaches for their problems (code). When they started applying their approaches to other problems (code), they realized that there are mainly a few basic problems (not the code itself, but its usage) that are faced while making code S.O.L.I.D. valid, and there were some set pieces of solutions for them. They documented/defined these solutions based on their problems (usage) and called them design patterns.
S.O.L.I.D. principles are a set of rules (guidelines) which define what qualities code should have to be called well-written, understandable, and maintainable code. Now, they (IDK who) came up with these principles by saying that code must NOT be:
Rigid → Hard to change.
Fragile → Minor change brings major issues.
Immobile → Cannot be used in other applications because of strong dependency on existing applications.
S.O.L.I.D. acronym stands for:
Single-Responsibility Principle → A class should have only 1 reason to change.
Open-Closed Principle → Software entities should be open for extension but closed for modifications.
Liskov’s Substitution Principle → A derived type must be interchangeable with other derived types of the same source.
Interface Segregation Principle → The use of an interface should not be forced on an entity.
Dependency Inversion Principle → High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
Types of types of Design Pattern:
-
Creational Design Patterns: These are the group of design patterns whose main focus in on object creation. They mainly focus on how objects are created, composed, and represented.
-
Behavioural Design Patterns: These patterns focuses on how the objects talk to each other efficiently and delegate their responsibilities.
-
Structural Design Patterns: These help in better structuring of the code for better readability and extensibility.
Creational Patterns
Here are some creational patterns:
Singleton Pattern
Singleton Pattern is used whenever only one instance of a class needs to be created and a global access point to that object needs to be provided. One implementation of Singleton pattern can be seen here. There are 2 implementations for Singleton patterns:
- Lazy instantiation using double locking mechanism: in which we initialize an instance of that class when required very first time, but to do this while also making it thread-safe, we make the thread creation part synchronized making it double locking mechanism.
- Early instantiation using implementation with static field: in which we create the instance when the class is loaded (not at first access, but well before that).
Factory Pattern
Factory Pattern creates objects by hiding the instantiation logic from the client. It refers to the created object through a common umbrella interface. I have implemented Factory Pattern here.
I will list all the design patterns as I learn and implement them myself.
References
Whatever is shown here and whatever will be added in the future is not meant to be in complete depth but here are some excellent resources:
|