- 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:

- Delphi程序設計基礎:教程、實驗、習題
- Hands-On Data Structures and Algorithms with JavaScript
- Vue.js 3.x從入門到精通(視頻教學版)
- Elastic Stack應用寶典
- C語言程序設計實踐教程
- Learning Python Design Patterns
- Python Data Analysis Cookbook
- 圖數據庫實戰
- Web前端應用開發技術
- 深入淺出Go語言編程
- 一步一步跟我學Scratch3.0案例
- Oracle實用教程
- NGUI for Unity
- Developing Java Applications with Spring and Spring Boot
- 程序員面試金典(第6版)