Skip to content

Instantly share code, notes, and snippets.

@benfluleck
Last active April 28, 2019 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfluleck/92dd345a8e1046be39bd2133a25f9eb6 to your computer and use it in GitHub Desktop.
Save benfluleck/92dd345a8e1046be39bd2133a25f9eb6 to your computer and use it in GitHub Desktop.
Single Responsibility
// 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