Created
January 18, 2019 04:01
-
-
Save rxluz/e04d2723fb8de31fa5e2232c62262b85 to your computer and use it in GitHub Desktop.
JS Design Patterns: Facade, see more at: https://medium.com/@_rxluz/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; | |
} | |
} | |
} | |
class ClientFacade { | |
constructor({ name, email, zipCode }) { | |
const ClientInstance = new Client({ | |
EmailValidationService: new SimpleEmailValidationService(), | |
ZipCodeValidationService: new SimpleZipCodeValidationService(), | |
name, | |
email, | |
zipCode, | |
}); | |
return ClientInstance; | |
} | |
} | |
const run = () => { | |
const ClientInstance = new ClientFacade({ | |
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