Skip to content

Commit 24cb839

Browse files
Add comprehensive practical applications guide
- Maps each pattern to real-world systems (Library, E-Commerce, ERP, Exam Platform) - Includes when to use / when NOT to use guidance - SOLID principles analysis for each pattern - Pattern selection decision tree and cross-pattern integration example
1 parent 541f3a8 commit 24cb839

1 file changed

Lines changed: 200 additions & 0 deletions

File tree

PRACTICAL_APPLICATIONS.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Practical Applications of Design Patterns
2+
3+
## Overview
4+
5+
This document demonstrates how the design patterns implemented in this repository
6+
can be applied to real-world software systems. Each pattern is mapped to practical
7+
use cases across different domains, helping developers understand **when** and **why**
8+
to use each pattern — not just **how**.
9+
10+
---
11+
12+
## 1. Observer Pattern (Behavioral)
13+
14+
### What It Does
15+
Allows an object (Subject) to notify multiple dependent objects (Observers)
16+
automatically when its state changes, without tight coupling between them.
17+
18+
### Real-World Applications
19+
20+
| System | Subject | Observers | Trigger |
21+
|--------|---------|-----------|---------|
22+
| Library Management | Book | Waiting users, Email service | Book returned |
23+
| E-Commerce | Product | Price alert subscribers | Price drops |
24+
| Social Media | User account | Followers | New post published |
25+
| Stock Trading | Stock price | Traders, Alert systems | Price changes |
26+
| IoT / Smart Home | Temperature sensor | AC unit, Dashboard, Phone app | Temperature changes |
27+
28+
### When to Use
29+
- When multiple parts of your system need to react to the same event
30+
- When you want to add new reactions without modifying the event source
31+
- When the number of dependent objects may change at runtime
32+
33+
### When NOT to Use
34+
- When there is only one object that needs to be notified
35+
- When the order of notification matters strictly
36+
- When observers need to respond synchronously in a guaranteed order
37+
38+
### SOLID Principles Applied
39+
- **Open/Closed Principle**: New observers can be added without modifying the Subject
40+
- **Dependency Inversion**: Subject depends on the Observer abstraction, not concrete classes
41+
- **Single Responsibility**: Each observer handles its own reaction logic
42+
43+
---
44+
45+
## 2. Null Object Pattern (Behavioral)
46+
47+
### What It Does
48+
Provides a default do-nothing object instead of returning None/null,
49+
eliminating the need for null checks throughout the codebase.
50+
51+
### Real-World Applications
52+
53+
| System | Real Object | Null Object | Benefit |
54+
|--------|-------------|-------------|---------|
55+
| Library Management | RegisteredUser | GuestUser | Guest can browse without errors |
56+
| E-Commerce | PremiumCustomer | NullCustomer | No crashes on missing accounts |
57+
| Logging System | FileLogger | NullLogger | Disable logging without code changes |
58+
| Payment System | CreditCardProcessor | NullProcessor | Skip payment in test mode |
59+
| Notification System | EmailSender | NullSender | Disable notifications cleanly |
60+
61+
### When to Use
62+
- When you find yourself writing `if obj is not None` repeatedly
63+
- When you want to provide a safe default behavior for missing objects
64+
- When None checks are scattered across multiple modules
65+
66+
### When NOT to Use
67+
- When None/null is a meaningful and expected state
68+
- When the absence of an object should genuinely raise an error
69+
- When performance is critical and even empty method calls matter
70+
71+
### SOLID Principles Applied
72+
- **Liskov Substitution**: NullObject can replace the real object anywhere
73+
- **Open/Closed Principle**: No need to modify existing code to handle null cases
74+
- **Interface Segregation**: Null object implements the same interface
75+
76+
### Code Comparison
77+
78+
**Without Null Object (fragile):**
79+
```python
80+
customer = find_customer(id)
81+
if customer is not None:
82+
if customer.email is not None:
83+
customer.send_notification("Hello")
84+
```
85+
86+
**With Null Object (clean):**
87+
```python
88+
customer = find_customer(id) # Returns NullCustomer if not found
89+
customer.send_notification("Hello") # Always safe
90+
```
91+
92+
---
93+
94+
## 3. Singleton Pattern (Creational)
95+
96+
### What It Does
97+
Ensures a class has only one instance throughout the entire program
98+
and provides a global access point to that instance.
99+
100+
### Real-World Applications
101+
102+
| System | Singleton Class | Why Only One? |
103+
|--------|----------------|---------------|
104+
| Any Application | DatabaseConnection | One connection pool shared everywhere |
105+
| Any Application | AppConfig | One consistent configuration source |
106+
| Online Exam Platform | ExamConfig | All modules read the same settings |
107+
| ERP System | LicenseManager | One license check for the whole system |
108+
| Game Engine | GameState | One game state shared by all systems |
109+
| Web Server | Logger | One log file, one writer |
110+
111+
### When to Use
112+
- When exactly one instance is needed to coordinate actions across the system
113+
- When that instance needs to be accessible from many different places
114+
- When creating multiple instances would waste resources or cause conflicts
115+
116+
### When NOT to Use
117+
- When you need multiple independent instances
118+
- When unit testing requires isolated instances (use dependency injection instead)
119+
- When the singleton holds mutable state that causes hidden coupling
120+
121+
### SOLID Principles Applied
122+
- **Single Responsibility**: The singleton manages its own instantiation
123+
- **Open/Closed Principle**: Subclasses can extend behavior while maintaining single instance
124+
125+
---
126+
127+
## 4. Specification Pattern (Behavioral)
128+
129+
### What It Does
130+
Encapsulates business rules as standalone objects that can be combined
131+
using boolean logic (AND, OR, NOT) to create complex selection criteria.
132+
133+
### Real-World Applications
134+
135+
| System | Specifications | Combined Rule Example |
136+
|--------|---------------|----------------------|
137+
| Inventory Management | LowStock, InCategory, FromSupplier | Low stock electronics from TechCorp |
138+
| E-Commerce | PriceBelow, InStock, HasDiscount | Cheap available items on sale |
139+
| Library Management | AvailableBook, InGenre, PublishedAfter | Available sci-fi books after 2020 |
140+
| HR System | InDepartment, SeniorityAbove, HasCertification | Senior certified engineers |
141+
| Banking | HighBalance, ActiveAccount, NoOverdraft | Eligible accounts for premium services |
142+
143+
### When to Use
144+
- When filtering logic is complex and changes frequently
145+
- When the same business rules are reused across different modules
146+
- When you need to combine rules dynamically at runtime
147+
148+
### When NOT to Use
149+
- When filtering logic is simple and unlikely to change
150+
- When you have only one or two conditions
151+
- When performance is critical and object creation overhead matters
152+
153+
### SOLID Principles Applied
154+
- **Single Responsibility**: Each specification encapsulates exactly one rule
155+
- **Open/Closed Principle**: New rules are added as new classes, no existing code changes
156+
- **Interface Segregation**: Each specification has one method: is_satisfied_by()
157+
158+
---
159+
160+
## Pattern Selection Guide
161+
162+
### How to Choose the Right Pattern
163+
164+
Do multiple parts of your system need to react to the same event?
165+
166+
└── YES → Observer Pattern
167+
Are you checking if something is None/null in many places?
168+
169+
└── YES → Null Object Pattern
170+
Do you need exactly one shared instance of a resource?
171+
172+
└── YES → Singleton Pattern
173+
Do you have complex, changeable filtering or validation rules?
174+
175+
└── YES → Specification Pattern
176+
177+
---
178+
179+
## Cross-Pattern Integration
180+
181+
In real-world systems, patterns work together. Here is an example of how
182+
all four patterns could be used in a single Library Management System:
183+
184+
- **Singleton**: `LibraryConfig` — one shared configuration for the entire system
185+
- **Observer**: `Book` notifies `WaitingUser` objects when returned
186+
- **Null Object**: `GuestUser` replaces None for unauthenticated visitors
187+
- **Specification**: `AvailableBookSpec.and_(InGenreSpec("science"))` filters the catalog
188+
189+
Each pattern solves a different problem, and together they create a clean,
190+
maintainable, and extensible architecture.
191+
192+
---
193+
194+
## References
195+
196+
- Gamma, E., Helm, R., Johnson, R., & Vlissides, J. (1994). *Design Patterns:
197+
Elements of Reusable Object-Oriented Software*. Addison-Wesley.
198+
- Martin, R. C. (2003). *Agile Software Development, Principles, Patterns,
199+
and Practices*. Prentice Hall.
200+
- Python Patterns Repository: https://github.com/faif/python-patterns

0 commit comments

Comments
 (0)