mituri

SOLID Dependency invesion principle

Feb 14th, 2021
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Dependency Inversion Principle */
  2.  
  3. /* high level modules should not depend on low level modules */
  4. /* abstractions should not depend on details */
  5.  
  6. // make transitions/changes is really hard in high level modules,so we should instead creade intermdiate api that wraps around low level module(f.e. Stripe)
  7.  
  8. // Good - we didnt have to change any code inside Shop, because we have payment processor interface, that allowing us to not have to change any of store code
  9. // all we have to do is create new payment processor, wrapping the API (Paypal, Stripe) we want to use and pass that into the new store()
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. /* BAD */
  17.  class Shop {
  18.   constructor(user) {
  19.     //this.stripe = new Stripe(user);
  20.    
  21.     // payment with paypal impl
  22.     this.paypal = new Paypal()
  23.     this.user = user
  24.   }
  25.  
  26.   purchaseBike(quantity) {
  27.     //this.stripe.makePayment(200 * quantity * 100)
  28.    
  29.     // payment with paypal impl
  30.     this.paypal.makePayment(this.user, 200 * quantity)
  31.    
  32.   }
  33.  
  34.   purchaseHelmet(quantity) {
  35.     //this.stripe.makePayment(15 * quantity * 100)
  36.    
  37.     // payment with paypal impl
  38.     this.paypal.makePayment(this.user, 15 * quantity)
  39.   }
  40. }
  41.  
  42. class Stripe {
  43.   constructor(user) {
  44.     this.user = user
  45.   }
  46.  
  47.   makePayment(amountInCents) {
  48.     console.log(`${this.user} made payment of  $${amountInCents / 100} with Stripe`)
  49.   }
  50. }
  51.  
  52. class Paypal {
  53.   makePayment(user, amountInDollars) {
  54.     console.log(`${user} made payment of $${amountInDollars} with Paypal`)
  55.   }
  56. }
  57.  
  58. const shop = new Shop('John')
  59. shop.purchaseBike(2)
  60. shop.purchaseHelmet(2)
  61.  
  62.  
  63.  
  64. /* GOOD */
  65. class Shop {
  66.     constructor(paymentProcessor) {
  67.     this.paymentProcessor = paymentProcessor;
  68.   }
  69.  
  70.   purchaseBike(quantity) {
  71.     this.paymentProcessor.pay(200 * quantity)
  72.   }
  73.  
  74.   purchaseHelmet(quantity) {
  75.     this.paymentProcessor.pay(15 * quantity)
  76.   }
  77. }
  78.  
  79. class StripePaymentProcessor {
  80.     constructor(user) {
  81.     this.stripe = new Stripe(user)
  82.   }
  83.  
  84.   pay(amountInDollars) {
  85.     this.stripe.makePayment(amountInDollars * 100)
  86.   }
  87. }
  88.  
  89. class PaypalPaymentProcessor {
  90.     constructor(user) {
  91.     this.user = user
  92.     this.paypal = new Paypal()
  93.   }
  94.  
  95.   pay(amountInDollars) {
  96.     this.paypal.makePayment(this.user, amountInDollars)
  97.   }
  98. }
  99.  
  100.  
  101. class Stripe {
  102.     constructor(user) {
  103.     this.user = user
  104.   }
  105.  
  106.   makePayment(amountInCents) {
  107.     console.log(`${this.user} made payment of  $${amountInCents / 100} with Stripe`)
  108.   }
  109. }
  110.  
  111. class Paypal {
  112.     makePayment(user, amountInDollars) {
  113.     console.log(`${user} made payment of $${amountInDollars} with Paypal`)
  114.   }
  115. }
  116.  
  117. /* const shop = new Shop(new StripePaymentProcessor("John")) */
  118. const shop = new Shop(new PaypalPaymentProcessor("John"))
  119. shop.purchaseBike(2)
  120. shop.purchaseHelmet(2)
  121.  
Advertisement
Add Comment
Please, Sign In to add comment