Skip to content

Instantly share code, notes, and snippets.

@kibolho
Created July 21, 2021 20:14
Show Gist options
  • Save kibolho/3d2efac77adfd2bf662c97805b9d8cd5 to your computer and use it in GitHub Desktop.
Save kibolho/3d2efac77adfd2bf662c97805b9d8cd5 to your computer and use it in GitHub Desktop.
SOLID - Interface Segregation Principle
// Bad Interface
interface Bird {
fly(): void;
walk(): void;
}
class Penguin implements Bird {
public fly() {
throw new Error('Unfortunately, Penguin can not fly!');
}
public walk() {
/// ...
}
}
class Hawk implements Bird {
public fly() {
/// ...
}
public walk() {
/// ...
}
}
// Good Interface
interface CanWalk {
walk(): void;
}
interface CanFly {
fly(): void;
}
class Penguin implements CanWalk {
public walk() {
/// ...
}
}
class Hawk implements CanFly, CanWalk {
public fly() {
/// ...
}
public walk() {
/// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment