Skip to content

Instantly share code, notes, and snippets.

@andreasonny83
Created February 17, 2019 22:09
Show Gist options
  • Save andreasonny83/c5b0caf4b6bf3baca013ac009c40cb0c to your computer and use it in GitHub Desktop.
Save andreasonny83/c5b0caf4b6bf3baca013ac009c40cb0c to your computer and use it in GitHub Desktop.
Facade pattern
/**
* The Facade pattern simplifies and hides the underlying complexity of a large abody of
* code by providing a cleaner and easy to use interface.
*/
class TaskService {
constructor(data){
this.name = data.name;
this.priority = data.priority;
this.project = data.project;
this.user = data.user;
this.completed = data.completed;
}
complete() {
this.completed = true;
console.log('completing task: ' + this.name);
}
setCompleteDate() {
this.completedDate = new Date();
console.log(this.name + ' completed on ' + this.completedDate);
}
notifyCompletion() {
console.log('Notifying ' + this.user + ' of the completion of ' + this.name);
}
save () {
console.log('saving Task: ' + this.name);
}
}
class TaskServiceFacade extends TaskService{
constructor(data){
super(data)
}
completeAndNotify(){
this.complete();
this.setCompleteDate();
this.notifyCompletion();
this.save();
}
}
let mytask = new TaskServiceFacade({
name: 'MyTask',
priority: 1,
project: 'Courses',
user: 'Jon',
completed: false
})
console.log(mytask.completeAndNotify())
// completing task: MyTask
// MyTask completed on Sun Jan 06 2019 09:54:33 GMT+0100 (West Africa Standard Time)
// Notifying Jon of the completion of MyTask
// saving Task: MyTask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment