Design Patterns
Used In
π Design Pattern Categories
π Definition
Design patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context. They help create reusable object-oriented software that is flexible, elegant, and maintainable.
"They reuse solutions that have worked for them in the past"
Key Benefits
- π― Specific to the problem at hand but general enough for future requirements
- π Reusable solutions that have been proven effective
- ποΈ Make object-oriented designs more flexible, elegant, and reusable
π¨ Core Principles
State Pattern
Represent states with objects
- Each possible state of an object is implemented as a separate class or object
- This allows an object to change its behavior depending on its current state
Decorator Pattern
- Decorate objects so you can easily add/remove features
Model-View Separation
Decoupling views from model β separating the UI (view) from the data and business logic (model)
- The model doesn't care how it's displayed
- The view doesn't care how data is fetched or processed
- They can change independently
π§ Popular Patterns
Popular Other Things
Guard Clauses Technique
Used to write , Cleaner and more readable code
void someFunction(){
if (wifi){
if(login){
if(admin){
someFunction();
} else {
// Handle non-admin user
return;
}
}else {
// Handle no login
return;
}
}else {
// Handle no wifi
return;
}
}
This change to
void someFunction(){
if (!wifi) {
// Handle no wifi
return;
}
if (!login) {
// Handle no login
return;
}
if (!admin) {
// Handle non-admin user
return;
}
someFunction();
}