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

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:

主站蜘蛛池模板: 洱源县| 临颍县| 浮山县| 牡丹江市| 铜陵市| 胶州市| 正镶白旗| 横山县| 莆田市| 元江| 资溪县| 兴化市| 江孜县| 定南县| 西贡区| 甘泉县| 乡宁县| 黄冈市| 凤翔县| 瑞金市| 杂多县| 上杭县| 贵州省| 定襄县| 霍林郭勒市| 桂平市| 运城市| 五原县| 沈阳市| 张家口市| 志丹县| 丹凤县| 昌吉市| 陕西省| 昆明市| 彭州市| 渝北区| 凭祥市| 宜城市| 瓦房店市| 本溪市|