Last active
January 18, 2019 02:36
-
-
Save rxluz/8b2475de8d1aca9a04865fc94d911145 to your computer and use it in GitHub Desktop.
JS Design Patterns: Adapter, see more at: https://medium.com/p/1a665e35a957
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 Users { | |
constructor(lang = "en") { | |
this.data = []; | |
this.language = lang; | |
this.languageMessages = { | |
add: { | |
en: "Added succefully", | |
fr: "Ajouté avec succès", | |
pt: "Adicionado com sucesso", | |
}, | |
remove: { | |
en: "Removed succefully", | |
fr: "Supprimé avec succès", | |
pt: "Removido com sucesso", | |
}, | |
}; | |
} | |
setLanguage(lang = "en") { | |
this.language = lang; | |
} | |
add(name) { | |
this.data.push(name); | |
console.log(this.languageMessages.add[this.language]); | |
return true; | |
} | |
remove(name) { | |
this.data = this.data.filter(currentName => name !== currentName); | |
console.log(this.languageMessages.remove[this.language]); | |
return true; | |
} | |
} | |
const run = () => { | |
const UsersInstance = new Users("fr"); | |
UsersInstance.add("Ricardo"); // will output: Ajouté avec succès | |
UsersInstance.setLanguage("en"); | |
UsersInstance.remove("Ricardo"); // will output: Removed succefully | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment