Last active
April 28, 2019 00:03
-
-
Save benfluleck/92dd345a8e1046be39bd2133a25f9eb6 to your computer and use it in GitHub Desktop.
Single Responsibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bad Case | |
class Staff { | |
constructor(staff) { | |
this.staff = staff; | |
} | |
registerStaff(staff) { | |
if (this.validateUser(staff)) { | |
return `${staff} has been created`; | |
} | |
return 'Error occured'; | |
} | |
validateStaff(sfaff) { | |
// mock validation | |
if (staff) { | |
return `${staff} validated`; | |
} | |
return `Sorry ${staff} could not validate user`; | |
} | |
} | |
// good | |
class ValidateUser { | |
constructor(user) { | |
this.user = user; | |
} | |
validate(user) { | |
if (user) { | |
return `${user} validated`; | |
} | |
return `Sorry could not validate user`; | |
} | |
} | |
class RegisterStaff { | |
constructor(staff) { | |
this.staff = staff; | |
this.validator = new ValidateStaff(staff); | |
} | |
createStaff(staff) { | |
if (this.validator.validate(staff)) { | |
return `${staff} has been created`; | |
} | |
return 'Error occured'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment