Dependency Inversion Principle with Example | Solid Principles
Dependency Inversion Principle with Example | Dependency Inversion Solid Principle | Dependency inversion principle | Dependency inversion | Solid dependency inversion | Dependency inversion principle javascript
Please refer above blog post to Understand SOLID principles in JavaScript. In this post, we will learn the Last Solid principle of Dependency Inversion with an Example in Javascript.
💡Let’s Create a Zoo Model
The zoo contains below all things.
1. There are Cages in the zoo.
2. There are Guards in the zoo
3. There are Humans in the zoo.
4. There is a Food zone in the zoo.
5. There are Animals in the zoo.
6. There are Vehicles in the zoo.
7. There are Trees and Plants in the zoo.
8. There are nests for several birds.
and many more things…
💻Now Design one of the things that is the Cage structure.
Cages have –
1. Bars – Wooden, Iron bars, small bars
2. Feeding Bowl – Fruits, water, meat.
3. size – height, width
▶️ Let’s first create an interface and classes for the above properties.
interface IBars {}
class WoodenBars implements IBars {}
class IronBars implements IBars {}
class SmallGapBars implements IBars {}
interface IFeedingBowl {}
class MeatFeedingBowl implements IFeedingBowl {}
class FruitFeedingBowl implements IFeedingBowl {}
class WaterFeedingBowl implements IFeedingBowl {}
interface ISize {}
class CageSize implements ISize {}
- carnivorous animals -मांसाहारी जानवर
herbivorous animals – शाकाहारी जानवर
class CarnivorousCage {
meetFeedingBowl: MeatFeedingBowl;
waterBowl: WaterFeedingBowl;
ironBars: IronBars;
smallGapBars: SmallGapBars;
listOfAnimals = new Array(5);
putAnimals() {
for (let i = 0; i < 5; i++) {
this.listOfAnimals.push('carnivorous animal' + i);
}
}
}
class HerbivorousCage {
fruitsFeedingBowl: FruitFeedingBowl;
waterBowl: WaterFeedingBowl;
woodenBars: WoodenBars;
listOfAnimals = new Array(5);
putAnimals() {
for (let i = 0; i < 5; i++) {
this.listOfAnimals.push('herbivorous animal' + i);
}
}
}
✔️ Composite Class – Classes that are made upon several other classes. CarnivorousCage & HerbivorousCage are composite classes because they are created with other classes.
✔️ Utility Class – FruitFeedingBowl, WoodenBars, IronBars, etc – are utility classes.
🤔 PROBLEM WITH THE ABOVE CODE –
1. For each type of cage, we have to create a new class.
2. Maximum codes are common in both classes, so this is a code duplicate.
Let’s try to understand the Dependencies in this structure –
💡 First Understand, what are High level and low-level designs?
High Level – They are generic and abstract and they may contain several things in self.
Low level – They are very specific and very details about one thing. It may be talking about only one thing.
Example –
College – High-level design
Medical college – low level (specific to medical studies only)
Eng College – low level
Clothes – High level
Jacket – low level
Coats – low level
So in the above code (Zoo) – find out low-level and high-level –
1. IFeedingBowl – High level
2. IBars – High level
3. FruitFeedingBowl – Low level (specific to fruits only)
4. MeatFeedingBowl – Low level
5. HerbivorousCage – High level
6. CarnivorousCage – High level
interfaces – High level (IBars, IFeedingBowl)
Implementations – Low level (FruitFeedingBowl, MeatFeedingBowl, WoodenBars)
Class – High level (HerbivorousCage, CarnivorousCage) – It seems specific but they are composite classes.
CarnivorousCage depends on three classes MeatFeedingBowl, WaterFeedingBowl, and IronBars and
they internally depend on other interfaces.
👀 Issue is here –
High-level modules or Classes depend on Low-level modules or details.
⭐ Dependency Inversion Principle ⭐
High-level modules should NOT depend on low-level modules/details.
Instead, they should depend on Abstractions (interfaces).
How can we achieve dependency inversion – by 💉 Dependency Injection?
Redesign Cage class using dependency inversion-
class Cage {
// dependency inversion
bowl: IFeedingBowl;
bars: IBars;
listOfAnimals: string[] = [];
// inject the dependencies in the constructor
constructor(bowl: IFeedingBowl, bars: IBars, animals: string[]) {
this.bowl = bowl;
this.bars = bars;
this.listOfAnimals = animals;
}
}
// Client will pass dependencies.
class Client {
main() {
var CarnivorousCage = new Cage(new MeatFeedingBowl(), new IronBars(), ['Tiger', 'Cheetah']);
var HerbivorousCage = new Cage(new FruitFeedingBowl(), new WoodenBars(), ['Horse', 'Monkey', 'Peecock']);
}
}
Dependency Inversion Principle with Example | Dependency Inversion Solid Principle | Dependency inversion principle | Dependency inversion | Solid dependency inversion | Dependency inversion principle javascript
- Implementation of Queue using Linked List | JavaScript
- Insert node in Linked list | Algorithm | JavaScript
- Insertion Sort in data structure | Algorithm with Examples
- Selection Sort Algorithm & K’th Largest Element in Array
- Quick Sort Algorithm with example | Step-by-Step Guide
- Dependency Inversion Principle with Example | Solid Principles
- Object-Oriented Programming | Solid Principles with Examples
- ASCII Code of Characters | String Operations with ASCII Code
- Negative Binary Numbers & 2’s Complement | Easy explanation
- Factors of a Number | JavaScript Program | Optimized Way
- LeetCode – Game of Life Problem | Solution with JavaScript
- Fibonacci series using Recursion | While loop | ES6 Generator
- JavaScript Coding Interview Question & Answers
- LeetCode – Coin Change Problem | Dynamic Programming | JavaScript
- HackerRank Dictionaries and Maps Problem | Solution with JavaScript
- React Redux Unit Testing of Actions, Reducers, Middleware & Store
- Micro frontends with Module Federation in React Application
- React Interview Question & Answers – Mastering React
- Top React Interview Question & Answer | React Routing
- React Interview Questions and Answers | React hooks
- Higher Order Component with Functional Component | React JS
- Top React Interview Questions and Answers | Must Know
- Interview Question React | Basics for Freshers and Seniors
- Cyber Security Fundamental Questions & Answers You must know
- Application & Web Security Interview Questions & Answers
- Top Scrum Master and Agile Question & Answers 2022
- Trapping Rain Water Leetcode Problem Solution
- Array Representation of Binary Tree | Full Tree & Complete Binary Tree
- Graphs in Data Structure, Types & Traversal with BFS and DFS, Algorithms
- Traversing 2 D array with BFS & DFS Algorithm in JavaScript
- Time Complexity & Calculations | All You should know
- Backspace String Compare Leetcode Problem & Solution
- Angular Interview Questions & Answers 2021 – Part 3 – JS Mount
- Why Angular is a Preferred Choice for Developers? Top Features
- Angular Interview Questions & Answers You should know Part 2
- Top 30 JavaScript Interview Questions and Answers for 2021
- React JS Stripe Payment Gateway Integration with Node | Step by Step Guide
- Create Year Month & Date dropdown List using JavaScript & JQuery
- Create Custom QR Code Component using QR Code Styling in React JS
- How to create a common Helper class or util file in React JS
- React Build Routing with Fixed Header and Navigation | React Router Tutorial
- React Create Dashboard Layout with Side Menu, Header & Content Area
- Web Application Security Best Practices | Top Tips to Secure Angular App
- HTML Form with Pure CSS & JavaScript | Custom Radio and Checkbox
- NgRx Top Interview Questions and Answers You should know
- Top 40 Awesome CSS Interview Questions & Answers You should know | CSS Tutorial
- Tips to Boost Angular App Performance | Web page Speed Optimization
- JavaScript Rotate 2D matrix 90 degrees clockwise | Top Interview Question
- HashTable Data Structure in JavaScript with Add Delete & Search Algorithms
- Trie Data Structure – Insert Search Delete & Print Program with JavaScript