Skip to content

Instantly share code, notes, and snippets.

@rxluz
Created January 18, 2019 04:01
Show Gist options
  • Save rxluz/e04d2723fb8de31fa5e2232c62262b85 to your computer and use it in GitHub Desktop.
Save rxluz/e04d2723fb8de31fa5e2232c62262b85 to your computer and use it in GitHub Desktop.
JS Design Patterns: Facade, see more at: https://medium.com/@_rxluz/c258ed53d7d7
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