Design Principle - Interface Segregation Principle

Definition

Interface Segregation Principle,即接口隔离原则。用多个专门的接口,而不是使用单一的总接口,客户端不应该依赖它不需要的接口。

一个类对另一个类的依赖应该建立在最小的接口上。
建立单一的接口,而不是接口庞大臃肿的接口。
尽可能细化接口,接口中的方法尽可能少。
注意适度原则。

Advantage

  • 提高了可读性。
  • 提高了拓展性。
  • 提高了可维护性。

Code Example

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle

Bird类不需要run()方法,Dog类也不需要fly()方法,基于接口隔离原则,进行接口细化。

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle

Design Principle - Interface Segregation Principle