Skip to content

Instantly share code, notes, and snippets.

@binary10ve
Created February 3, 2019 10:53
Show Gist options
  • Save binary10ve/f41d7ff5a379033e7c6a5c88acb91697 to your computer and use it in GitHub Desktop.
Save binary10ve/f41d7ff5a379033e7c6a5c88acb91697 to your computer and use it in GitHub Desktop.
class Phone{
backup(){
console.log("Applying default backup strategy")
}
}
function googleBackup(phone){
const _backup = phone.backup;
phone.backup = function(){
console.log("Applying google backup stragegy")
_backup.call(phone)
}
}
function boxBackup(phone){
const _backup = phone.backup;
phone.backup = function(){
console.log("Applying box backup stragegy")
_backup.call(phone)
}
}
const p = new Phone();
googleBackup(p)
boxBackup(p)
p.backup()
/**
Advantages:
The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime.
The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original class; decorating can provide new behavior at runtime for individual objects.
Decorator offers a pay-as-you-go approach to adding responsibilities. Instead of trying to support all foreseeable features in a complex, customizable class, you can define a simple class and add functionality incrementally with Decorator objects.
Disadvantages:
Decorators can complicate the process of instantiating the component because you not only have to instantiate the component, but wrap it in a number of decorators.
It can be complicated to have decorators keep track of other decorators, because to look back into multiple layers of the decorator chain starts to push the decorator pattern beyond its true intent.
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment