Design Patterns:
No More Spaghetti Code 🍝
What are Design Patterns?
1. Typical solution for occurring problem.
2. Not copy/paste and run.
3. General Concept, not algorithm.
Algorithm
Design Patterns
Why Design Patterns?
1. Tried and tested solution.
2. Teaches principles of OOD (Object Oriented Design).
3. Common language.
When?
1. When your code feels like spaghetti 🍝 (hard to read/maintain).
2. When multiple developers need to collaborate (common language).
3. When scaling your app (patterns ensure flexibility).
Categories
Creational Behavioral Structural
Observer
Strategy
Adapter
Decorator
Factory
Singleton
Benefits you’ll be getting
How?
Why?
What?
Which Patterns for today?
1. Singleton 🚪 One Door
2. Factory Coffee Maker
☕
3. Observer 📢 News Alerts
Quiz Time❓
Categories
Creational Behavioral Structural
Observer
Strategy
Adapter
Decorator
Factory
Singleton
Factory Pattern
Factory
What: Centralize object creation.
When:
- Object creation logic is complex
- Multiple object types (e.g., UI components, payment gateways).
How:
- Factory class with create() method.
- Decouples client code from concrete classes.
Gotchas:
- Avoid for simple object creation.
Why Use the Factory Pattern?
- Encapsulation of Object Creation
- Loose Coupling
- Improved Maintainability
- Scalability
Types of Factory Patterns
- Simple Factory (Static Factory Method)
- Factory Method Pattern
- Abstract Factory Pattern
Gonggi Time
Categories
Creational Behavioral Structural
Observer
Strategy
Adapter
Decorator
Factory
Singleton
Singleton Pattern
1. Every time a class is instantiated, a new object is created
2. Unnecessary memory usage and performance issues
3. Risk of data conflicts when sharing resources
Problem
1. Ensures only one instance of a class is created.
2. The same object is shared across the application.
3. Prevents data conflicts and saves resources.
Solution - Singleton
How:
- Private constructor.
- Static instance method.
Gotchas:
- Overuse Tight coupling.
→
- Multithreading risks.
1. Concurrent Access – Avoids conflicts when multiple processes access the object
2. Global Access Point – A single access point for consistency
3. Single Object – Centralized control and easier management
Why Use Singleton?
Wordle Time 🔠
Categories
Creational Behavioral Structural
Observer
Strategy
Adapter
Decorator
Factory
Singleton
Observer Pattern
Observer
What: Subscribe/notify for state changes.
When:
- Dynamic updates (e.g., chat apps, stock prices).
- Decoupling components (e.g., UI backend).
↔
How:
- Subject (youtuber) + Observers (subscribers).
Gotchas:
- Memory leaks (forget to unsubscribe).
What NOT to Do: Anti-Patterns 🚫
1. Monolithic Class: One class doing everything.
2. Copy-Paste Programming: Duplicate code.
3. Pattern Overkill: Using Singleton for everything.
Challenge Time🏆

GDGoC: BZU - Design Patterns: No More Spaghetti Code