The beauty of design patterns 16: What are the three applications of the interface isolation principle? How to understand the interface

2020/02/0411:48:21 technology 1578

Wang Zheng "The Beauty of Design Patterns" notes How does

understand the "interface segregation principle"? The English translation of

interface segregation principle is "Interface Segregation Principle", abbreviated as ISP. Meaning: The client should not be forced to rely on interfaces it does not need. The "client" can be understood as the caller or user of the interface.

The key to understanding the principle of interface isolation is to understand the word "interface". In this principle, we can understand "interface" as the following three things:

UserService When the user delete function is added in, the new RestrictedUserService is added for specific users.

understands the "interface" as a single API interface or function

understands the "interface" as the interface concept in OOP

through the hot update of RedisConfig, MysqlConfig, KafkaConfig and display on the page, it shows that it conforms to the principle of interface isolation Code:

1. The design ideas are more flexible, easy to extend, and easy to reuse.

2. The design ideas that do not conform to the code implementation have done some useless work

Focus on review

1. How to understand the "interface isolation principle"?

understand the "interface isolation principle" The point is to understand the word "interface". There are three different understandings here. If

understands "interface" as a set of interfaces, it can be the interface of a certain microservice or the interface of a certain class library. If part of the interface is only used by some callers, we need to isolate this part of the interface and use it alone for this part of the caller, without forcing other callers to also rely on this part of the interface that will not be used.

If "interface" is understood as a single API interface or function, and some callers only need part of the function in the function, then we need to split the function into multiple functions with finer granularity, so that the caller only depends on what it needs That fine-grained function. If

understands "interface" as an interface in OOP, it can also be understood as an interface syntax in an object-oriented programming language. The design of the interface should be as simple as possible, and don't let the implementation class and caller of the interface rely on unnecessary interface functions.

2. The difference between the interface isolation principle and the single responsibility principle

The single responsibility principle is aimed at the design of modules, classes, and interfaces. Compared with the single responsibility principle, the interface isolation principle focuses more on the design of the interface on the one hand, and on the other hand, its thinking angle is different. The principle of interface isolation provides a standard for judging whether the responsibility of an interface is single: indirectly judged by how the caller uses the interface. If the caller only uses part of the interface or part of the function of the interface, then the design of the interface is not single enough.

reference: https://time.geekbang.org/column/intro/250?code=gLit0LpsKZQ6vOVqS1htGOSAKYLCYeMuklw2dwajH-4%3D

technology Category Latest News