Created
January 18, 2019 03:53
-
-
Save rxluz/b4478b5c3d865a7d0acb7e10e8b2140e to your computer and use it in GitHub Desktop.
JS Design Patterns: Facade, see more at: https://medium.com/p/c258ed53d7d7
This file contains hidden or 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
class SimpleEmailValidationService { | |
isValid(email) { | |
return email.indexOf("@") > 2; | |
} | |
} | |
class SimpleZipCodeValidationService { | |
isValid(zipCode) { | |
//A98 HNA23 | |
return zipCode.length === 8; | |
} | |
} | |
class Client { | |
constructor({ | |
EmailValidationService, | |
ZipCodeValidationService, | |
name, | |
email, | |
zipCode, | |
}) { | |
this._emailValidationService = EmailValidationService; | |
this._zipCodeValidationService = ZipCodeValidationService; | |
this.setName(name); | |
this.setEmail(email); | |
this.setZipCode(zipCode); | |
} | |
setName(name) { | |
this.name = name; | |
} | |
setEmail(email) { | |
if (this._emailValidationService.isValid(email)) { | |
this.email = email; | |
} | |
} | |
setZipCode(zipCode) { | |
if (this._zipCodeValidationService.isValid(zipCode)) { | |
this.zipCode = zipCode; | |
} | |
} | |
} | |
const run = () => { | |
const ClientInstance = new Client({ | |
EmailValidationService: new SimpleEmailValidationService(), | |
ZipCodeValidationService: new SimpleZipCodeValidationService(), | |
name: "Big Client Corporation", | |
email: "[email protected]", | |
zipCode: "A98 HN23", | |
}); | |
console.log(ClientInstance); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment