Skip to main content

Interface Segregation Principle

介面隔離原則

分離的操作

有問題的架構

Example

  • Bad way:
interface Bird {
fly(): void;
walk(): void;
}

class Nightingale implements Bird {
public fly() {
/// ...
}
public walk() {
/// ...
}
}

Kiwi 奇異鳥不會飛!!!

  • 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() {
/// ...
}
}