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

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:

主站蜘蛛池模板: 玉龙| 隆化县| 洛川县| 手游| 湘乡市| 富顺县| 江城| 南澳县| 邹城市| 温州市| 河间市| 荣昌县| 泸溪县| 乾安县| 兴安盟| 平武县| 建湖县| 江孜县| 兴国县| 永康市| 水富县| 余姚市| 华蓥市| 买车| 乌恰县| 确山县| 民乐县| 南昌市| 禹州市| 中西区| 原阳县| 巧家县| 甘泉县| 红河县| 隆子县| 潮安县| 邵阳县| 从化市| 石门县| 镇宁| 红桥区|