Skip to content

Instantly share code, notes, and snippets.

@benfluleck
Created April 28, 2019 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfluleck/5486f1a476e197b7fdbba5066d6f504e to your computer and use it in GitHub Desktop.
Save benfluleck/5486f1a476e197b7fdbba5066d6f504e to your computer and use it in GitHub Desktop.
Interface segregation principle
// bad
class Post {
constructor(){
}
editPost(){
console.log('Post edited')
}
createPost(){
console.log('Post created')
}
deletePost(){
console.log('Post created')
}
}
// moderators edit posts
// the moderator classes does not depend on the other functions
// but now it has access to the other methods
class Moderator extends Post () {
}
// good
class Post {
constructor(moderator){
this.moderator = moderator
}
editPost(){
if(this.moderator) {
console.log('Post edited')
} else {
console.log('Post denied')
}
}
createPost(){
console.log('Post created')
}
deletePost(){
console.log('Post created')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment