官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 垣曲县| 喜德县| 兴和县| 襄垣县| 舒城县| 波密县| 中超| 富裕县| 双城市| 固始县| 临泉县| 日照市| 齐齐哈尔市| 泸西县| 大方县| 南木林县| 景东| 二连浩特市| 江北区| 南郑县| 炉霍县| 桐梓县| 南江县| 炉霍县| 开封县| 大同市| 普定县| 建水县| 台前县| 喀喇沁旗| 鄂尔多斯市| 辛集市| 林周县| 淳化县| 高州市| 四川省| 化德县| 扎囊县| 肇州县| 黑水县| 昆山市|