mituri

SOLID Single responsibility principle

Feb 14th, 2021
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* S.O.L.I.D. */
  2.  
  3. /* Single responsiblitiy prinicple
  4. - everything should only have one reason to change */
  5.  
  6. /* BAD */
  7. class UserSettings {
  8.   constructor(user) {
  9.     this.user = user
  10.   }
  11.  
  12.   changeSettings(settings) {
  13.   if (this.verifyCredentials()) {
  14.     //...
  15.     }
  16.   }
  17.  
  18.   verifyCredentials() {
  19.     //...
  20.   }
  21. }
  22.  
  23. /* GOOD */
  24. class UserAuth {
  25.   constructor(user) {
  26.     this.user = user
  27.   }
  28.  
  29.   verifyCredentials() {
  30.     //...
  31.   }
  32. }
  33.  
  34. class UserSettings {
  35.     constructor(user) {
  36.   this.user = user;
  37.   this.auth = new UserAuth(user);
  38.   }
  39.  
  40.   changeSettings(settings) {
  41.     if (this.auth.verifyCredentials()) {
  42.         //...
  43.     }
  44.   }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment