1. main
1.1. Understanding Dependency Control: Anti-Patterns and Best Practices
1.1.1. In software design, dependency control is essential to ensure that
1.1.1.1. objects are flexible,
1.1.1.1.1. be able to move things aroun d
1.1.1.2. maintainable,
1.1.1.3. and testable.
1.1.1.4. scalable
1.1.2. This blog will walk you through common anti-patterns and their solutions, starting from tightly coupled designs and progressing to more flexible ones using Dependency Injection (DI).
1.1.3. Objection
1.1.3.1. not a solution to everything
1.1.3.1.1. decoupling with message bus
1.1.3.1.2. or simple google extension
1.1.3.2. Boiler plate
1.1.4. We will look at:
1.1.4.1. Tightly coupled dependencies.
1.1.4.2. Child depending on the parent.
1.1.4.3. The God Object anti-pattern.
1.1.4.4. Decoupling via callback functions.
1.1.4.5. Callback function drilling.
1.1.4.6. Flexible dependency management using Dependency Injection.
1.2. Progression
1.2.1. 1. Simple Dependency (Tight Coupling)
1.2.1.1. Visualization
1.2.1.2. The Problem:
1.2.1.2.1. In this example, the Parent directly creates and manages an instance of the Child.
1.2.1.2.2. Why It's an Issue:
1.2.1.3. This is ok for simple stuff, though. No need to make it complicated all the time
1.2.1.3.1. kiss keep it simple stupid
1.2.2. 2. Child Depending on Parent
1.2.2.1. Visualization
1.2.2.2. The Problem:
1.2.2.2.1. In this scenario, the Child directly depends on the Parent.
1.2.2.2.2. The Parent passes itself to the Child, making the child aware of and dependent on the Parent's methods.
1.2.2.2.3. Why It's an Issue:
1.2.2.3. Solution:
1.2.2.3.1. A better approach is to decouple the child from the parent by using callback functions or dependency injection.
1.2.3. 3. The God Object Anti-Pattern
1.2.3.1. visualization
1.2.3.1.1. theApp
1.2.3.2. The Problem:
1.2.3.2.1. Why It's an Issue:
1.2.3.2.2. A God Object is an object that takes on too many responsibilities, handling everything from creating dependencies to managing relationships between other objects.
1.2.3.2.3. In this example, the Parent is the God Object because it controls all of its children and their interactions.
1.2.3.2.4. when god objects
1.2.4. 4. Callback Solution (Decoupling with Callbacks)
1.2.4.1. vizualization
1.2.4.1.1. loose coupling with callback functions
1.2.4.1.2. two way
1.2.4.2. The Solution:
1.2.4.2.1. To decouple the Parent and Child, you can use callback functions. Instead of having the Child directly depend on the Parent, the Parent passes a function to the Child to call when it needs help.
1.2.4.2.2. Why It's Better:
1.2.4.3. Problem
1.2.4.3.1. 5. Callback Drilling
1.2.4.4. This is ok for simple stuff, though. No need to make it complicated all the time
1.2.4.4.1. kiss keep it simple stupid
1.2.5. 6. Dependency Injection (Loosely Coupled)
1.2.5.1. The Solution:
1.2.5.1.1. visual
1.2.5.1.2. Objection
1.2.5.1.3. Dependency Injection (DI) is a pattern that injects dependencies into objects from the outside.
1.2.5.1.4. Why Dependency Injection (DI) is the Best Approach:
1.3. Conclusion:
1.3.1. In this blog, we explored various patterns and anti-patterns for managing dependencies between objects in software design.
1.3.1.1. We started with tightly coupled code, where the Parent directly created and managed its Child, and worked our way towards more flexible and scalable solutions using Dependency Injection (DI) with InversifyJS.
1.3.2. Key Takeaways:
1.3.2.1. Tight Coupling makes systems hard to maintain, test, and extend. It should be avoided by decoupling object creation and management.
1.3.2.2. God Objects over-centralize control and should be avoided by delegating responsibilities to smaller, more focused objects.
1.3.2.3. Callback Functions offer a step towards decoupling, but they can lead to complexity when passed through multiple layers (callback drilling).
1.3.2.4. Dependency Injection (DI) is a powerful solution to decouple dependencies, improve testability, and make systems more maintainable.