Interface Segregation Principle
Example
- Bad way:
interface Bird {
fly(): void;
walk(): void;
}
class Nightingale implements Bird {
public fly() {
/// ...
}
public walk() {
/// ...
}
}
奇異鳥不會飛!!!
- Good way:
class Kiwi implements Bird {
public fly() {
throw new Error("Unfortunately, Kiwi can not fly!");
}
public walk() {
/// ...
}
}
interface CanWalk {
walk(): void;
}
interface CanFly {
fly(): void;
}
class Nightingale implements CanFly, CanWalk {
public fly() {
/// ...
}
public walk() {
/// ...
}
}
class Kiwi implements CanWalk {
public walk() {
/// ...
}
}