Last active
August 20, 2020 14:14
-
-
Save yeisoncruz16/727c76f7c7a30506a5c3a931d9073a2f to your computer and use it in GitHub Desktop.
Simple chain of responsibility pattern implemented using Javascript ES6
This file contains 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 BaseDesign { | |
setNext(next){ | |
this._next = next; | |
} | |
next(role){ | |
if(this._next){ | |
return this._next.run(role); | |
} | |
return `There are not Design for role ${role}`; | |
} | |
} | |
class DesignOne extends BaseDesign { | |
run(role){ | |
if(this.isMyResponsability(role)){ | |
// complex logic go here | |
return "DesignOne"; | |
} | |
return this.next(role); | |
} | |
// complex logic | |
isMyResponsability(role){ | |
return role == 1; | |
} | |
} | |
class DesignTwo extends BaseDesign { | |
run(role){ | |
if(this.isMyResponsability(role)){ | |
// complex logic go here | |
return "DesignTwo"; | |
} | |
return this.next(role); | |
} | |
isMyResponsability(role){ | |
return role == 2; | |
} | |
} | |
class DesignThree extends BaseDesign { | |
run(role){ | |
if(this.isMyResponsability(role)){ | |
// complex logic go here | |
return "DesignThree"; | |
} | |
return this.next(role); | |
} | |
isMyResponsability(role){ | |
return role == 3; | |
} | |
} | |
const designOne = new DesignOne(); | |
const designTwo = new DesignTwo(); | |
const designThree = new DesignThree(); | |
designTwo.setNext(designThree); | |
designOne.setNext(designTwo); | |
let role = 1; | |
const finalDesing = designOne.run(role); | |
// complex logic go here | |
console.log(finalDesing); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment