Skip to content

Commit

Permalink
add dependency inversion principle
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitmehrotra committed Feb 12, 2023
1 parent b10c967 commit 335d6a2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
8. **Liskov Substitution Principle** - This principle states that anywhere you use one type of class, you need to be able to use all type of subclasses of that class and it should work just fine.

9. **Interface Segregation Principle** - The main idea of the interface segregation principle is that any class that implements an interface must use all functions/properties of the interface. JavaScript does not have actual interfaces, but a class inheritance structure is similar enough. This means that any class that inherits from another class needs to use all of the methods/properties from the base class. This encourages writing small classes instead of large classes.

10. **Dependency Inversion Principle** - The main idea of the dependency inversion principle is that any class that uses a dependency should only ever use the dependency through a predefined interface/wrapper. This makes it so that your code will never directly depend on a low level API for its operations. The reason this is so important is because if you ever need to change or remove that dependency it becomes really difficult when it is used all over your code. By wrapping this dependency in an interface you can depend on the interface you created which will make changing out the dependency painless.
57 changes: 57 additions & 0 deletions dependency-inversion-principle/after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Store {
constructor(paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}

purchaseBike(quantity) {
this.paymentProcessor.pay(200 * quantity);
}

purchaseHelmet(quantity) {
this.paymentProcessor.pay(15 * quantity);
}
}

class StripePaymentProcessor {
constructor(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 payment of $${amountInCents / 100} with Stripe`
);
}
}

class PaypalPaymentProcessor {
constructor(user) {
this.user = user;
this.paypal = new Paypal();
}

pay(amountInDollars) {
this.paypal.makePayment(this.user, amountInDollars);
}
}

class Paypal {
makePayment(user, amountInDollars) {
console.log(`${user} made payment of $${amountInDollars} with Paypal`);
}
}

// const store = new Store(new StripePaymentProcessor("John"));
const store = new Store(new PaypalPaymentProcessor("John"));
store.purchaseBike(2);
store.purchaseHelmet(2);
39 changes: 39 additions & 0 deletions dependency-inversion-principle/before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Store {
constructor(user) {
this.paypal = new Paypal();
this.user = user;
// this.stripe = new Stripe(user);
}

purchaseBike(quantity) {
// this.stripe.makePayment(200 * quantity * 100);
this.paypal.makePayment(this.user, 200 * quantity);
}

purchaseHelmet(quantity) {
// this.stripe.makePayment(15 * quantity * 100);
this.paypal.makePayment(this.user, 15 * quantity);
}
}

class Stripe {
constructor(user) {
this.user = user;
}

makePayment(amountInCents) {
console.log(
`${this.user} made payment of $${amountInCents / 100} with Stripe`
);
}
}

class Paypal {
makePayment(user, amountInDollars) {
console.log(`${user} made payment of $${amountInDollars} with Paypal`);
}
}

const store = new Store("John");
store.purchaseBike(2);
store.purchaseHelmet(2);

0 comments on commit 335d6a2

Please sign in to comment.