Design Patterns Lab — Lecture 5: Singleton & Dependency Injection (DI)
In this lecture, we explore Singleton and Dependency Injection (DI) as two connected design concepts that address critical problems in backend systems: shared resources, object creation, and system flexibility. We begin with a realistic backend scenario where multiple services depend on the same data or configuration. As the system grows, we observe how naive designs lead to duplication, inconsistent state, and poor separation of responsibilities. Step by step, we analyze: - Why uncontrolled object creation leads to duplication and inconsistency - How Singleton ensures a single shared instance across the system - Why Singleton introduces tight coupling - Why creating dependencies inside classes is a design flaw - How Dependency Injection separates object creation from business logic - How DI restores flexibility, scalability, and testability This lecture focuses on understanding the problem → solution → new problem → final solution journey, helping you apply these patterns correctly in real-world systems. 📚 Topics covered: • Shared resource problems in backend systems • Singleton Pattern — concept and real meaning • Ensuring system-wide consistency • Memory duplication and expensive operations • Tight coupling as a design issue • Dependency Injection (DI) — core principle • Hidden dependencies and poor design • Constructor-based injection • Abstraction and loose coupling • Separating object creation from usage • Testing advantages with DI • Singleton vs Dependency Injection (clear comparison) 💡 Key Takeaways: - Singleton ensures a single shared instance across the system - It solves duplication and consistency issues - It introduces tight coupling if used alone - Dependency Injection removes coupling and improves flexibility - DI makes systems easier to test, extend, and maintain - Good design evolves by solving problems step by step 📂 Repository (code + activities): https://github.com/Maryam-Skaik/design-patterns-lab-singleton https://github.com/Maryam-Skaik/design-patterns-lab-dependency-injection 🧪 Practice Activity: - Refactor a tightly coupled service using Dependency Injection - Identify and fix shared resource issues using Singleton
Download
0 formatsNo download links available.