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

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:

主站蜘蛛池模板: 旺苍县| 鸡西市| 丹巴县| 新野县| 嘉兴市| 紫阳县| 邓州市| 尼勒克县| 龙井市| 西乌| 宣恩县| 梨树县| 聂拉木县| 曲水县| 高雄市| 苗栗县| 鱼台县| 察隅县| 石楼县| 连城县| 高台县| 小金县| 东阳市| 蕉岭县| 胶州市| 唐河县| 沙河市| 贡觉县| 北京市| 清水河县| 长治市| 伽师县| 桦川县| 吴桥县| 郸城县| 沽源县| 神池县| 雅江县| 宁武县| 黄山市| 新蔡县|