- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 210字
- 2021-06-25 20:52:30
Interface Segregation Principle
The following quote is taken from https://www.oodesign.com/interface-segregation-principle.html link:
"Clients should not be forced to depend upon interfaces that they don't use."
When applied, the Interface Segregation Principle (ISP) reduces the code coupling, making the software more robust, and easier to maintain and extend. ISP was first announced by Robert Martin, when he realized that if the principle is broken and clients are forced to depend on interfaces they don't use, the code becomes so tightly coupled that it's almost impossible to add new functionality to it.
In order to better understand this, let's again take the car-service example (refer to the following diagram). Now we need to implement a class named Mechanic. The mechanic repairs cars, so we add a method of repair car. In this case, the Mechanic class depends upon the I class. However, the Car class exposes a richer sets of methods than the Mechanic class needs:

This is a bad design because if we want to replace a car with another one, we need to make changes in the Mechanic class, which violates the open/closed principle. Instead, we must create an interface that exposes only the relevant methods required in the Mechanic class, as shown in the following diagram:

- Oracle Database In-Memory(架構與實踐)
- Vue.js快速入門與深入實戰
- 微服務設計原理與架構
- MySQL數據庫管理與開發(慕課版)
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- Android Wear Projects
- SSM開發實戰教程(Spring+Spring MVC+MyBatis)
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- Java并發編程之美
- 遠方:兩位持續創業者的點滴思考
- Redmine Cookbook
- Building Clouds with Windows Azure Pack
- Android項目實戰:博學谷
- Java Web應用開發