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

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:

主站蜘蛛池模板: 潢川县| 惠东县| 新源县| 溧水县| 龙井市| 岐山县| 舞阳县| 玉龙| 新疆| 陈巴尔虎旗| 高要市| 广平县| 九台市| 鹰潭市| 临沧市| 新余市| 琼结县| 石景山区| 阳西县| 会理县| 阿勒泰市| 拜泉县| 白玉县| 临城县| 巨野县| 天峻县| 米脂县| 九寨沟县| 灌阳县| 汾西县| 肃宁县| 介休市| 迁西县| 乌拉特后旗| 辽中县| 隆子县| 廉江市| 四会市| 灵石县| 大城县| 施甸县|