Dependency Inversion Principle Explained - SOLID Design Principles
Why Stack Overflow recommends it
Stack Overflow users link it when asking about dependency inversion in JavaScript, since its Store and PaymentProcessor example is the code they build their questions around.
Recommended by 2 people across 2 different months, 2022 to 2023.
What it is
A 13-minute explainer from Web Dev Simplified on the Dependency Inversion Principle, one of the SOLID design principles. It uses a JavaScript store and payment processor example to show why code should depend on an interface or wrapper instead of a low-level API, and how to spot violations.
What people said
Recommends ElMoscaviador · 2023-04 · Stack Overflow · on “Dependency Inversion Principle: is a low level module allowed to have a hidden reference to another low level module?”
Consider the following code from Web Dev Simplified's video on the Dependency Inversion Principe: [this video] class Store { constructor(paymentProcessor) { this.paymentProcessor = paymentProcessor; } purchase(quantity) { this.paymentProcessor.pay(200 * quantity); } } class StripePaymentProcessor { constructor(user) { this.user = user; this.stripe = new Stripe(user); } pay(amountInDollars) { this.stripe.makePayment(amountInDollars * 100); } } class Stripe { constructor(user) { this.user = user; } makePayment(amountInCents) { console.log( `${this.user} made a payment of $${amountInCents / 100} with Stripe` ); } } class PayPalPaymentProcessor { constructor(user) { this.user = user; this.paypal = ne
Recommends Littlee · 2023-06 · Stack Overflow · on “dependency inversion implementation in javascript”
the book "Clean Architecture" said: When using dynamically typed languages like Ruby and Python ... dependency inversion does not require either the declaration or the inheritance of interfaces. but I don't quite understand it, the book didn't provide any example my understanding of the DIP is that if ClassA depends on ClassB violating the DIP, we can introduce an InterfaceC, then we get something like ClassA ---> InterfaceC <--- ClassB I've also looked at some examples on the internet, like this one that attempts dependency injection between PurchaseHandler and PayPal , [LINK] class PurchaseHandler { processPayment(paymentDetails, amount) { const paymentSuccess = PaymentHandler.requestPayme
Cites JohnJS · 2022-08 · Stack Overflow · on “How can I remove my if else conditions from my code and apply dynamic behavior when I add new logic?”
I have the following code class Store { constructor(paymentProcessor) { this.paymentProcessor = paymentProcessor; } purchaseBike(price, quantity) { if(this.paymentProcessor instanceof StripePaymentProcessor) { this.paymentProcessor.pay(price * quantity); } else { let token = generateToken(); this.paymentProcessor.pay(price * quantity, token, new Date()); } } purchaseHelmet(price, quantity) { if(this.paymentProcessor instanceof StripePaymentProcessor) { this.paymentProcessor.pay(price * quantity); } else { let token = generateToken(); this.paymentProcessor.pay(price * quantity, token, new Date()); } } } function generateToken() { return 'sdsd1212zwsd'; } class StripePaymentProcessor { constru