In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the interface isolation principle of the PHP object-oriented five principles, which is very detailed and has a certain reference value. Interested friends must read it!
The details are as follows:
When designing an application, if a module contains multiple sub-modules, then we should be careful to abstract the module. Imagine that the module is implemented by a class, and we can abstract the system into an interface. But when adding a new module extension, if the module to be added contains only some sub-modules in the original system, then the system will force us to implement all the methods in the interface and write some dumb methods. Such interfaces are called fat interfaces or contaminated interfaces, and the use of such interfaces will introduce some improper behavior to the system, which may lead to incorrect results or lead to a waste of resources.
1. Interface isolation
The principle of interface isolation (Interface Segregation Principle, ISP) states that clients should not be forced to implement interfaces that they will not use, but should group methods in fat interfaces and replace them with multiple interfaces, each serving a submodule. Simply put, it is much better to use multiple specialized interfaces than to use a single interface.
The main points of ISP are as follows:
1) the dependency of one class on another class should be based on the smallest interface.
ISP can achieve that customers (the way the interface is used) are not forced to rely on methods they do not use, and the implementation class of the interface should only be presented as a single responsibility role (following the SRP principle).
ISP can also reduce customer interaction-when one customer requests a new responsibility (which requires a change) and forces a change in the interface, it is least likely to affect other clients.
2) the client program should not rely on interface methods (functions) that it does not need.
The client program should rely on interface methods (functions) that it does not need, so what does it depend on? Rely on the interface it needs. What interface the client needs is what interface is provided, and the unneeded interface is removed, which requires refinement of the interface to ensure its purity.
For example, when inheriting, because the subclass inherits all the methods available in the parent class, some methods in the parent class may not be needed in the subclass. For example, both the average employee and the manager inherit from the employee interface, and the employee needs to write a work log every day, but the manager does not. Therefore, you cannot use the work log to card the manager, that is, the manager should not rely on the method of submitting the work log.
It can be seen that there is some conceptual overlap between ISP and SRP. In fact, many design patterns overlap in concept, and it is even difficult for you to tell which design pattern a piece of code belongs to.
What ISP emphasizes is that the less commitment the interface has to the client, the better, and to be single-minded. When the requirements of one client program change and the interface is forced to change, it is less likely to affect other clients. This is actually the problem of interface pollution.
two。 Contamination of the interface
Overbloated interface design is a pollution to the interface. The so-called interface pollution is to add unnecessary responsibilities to the interface, if the developer adds a new feature to the interface only to reduce the number of interface implementation classes, then this design will cause the interface to be constantly "contaminated" and "fat".
"Interface isolation" is actually the principle of customized service design. The use of multiple inheritance of interfaces to achieve the combination of different interfaces, so as to provide composite functions-to achieve "on-demand services".
The interface should be disassembled, but not too fine, so there must be a standard, that is, high cohesion. The interface should have some basic functions and be able to accomplish a basic task alone.
In practical application, we will encounter the following problems: for example, I need a DAO implementation that can adapt to many types of databases, then I should first implement an interface for database operation, which specifies some basic methods of database operation, such as connecting to the database, adding, deleting, changing and querying, closing the database, and so on. This is a least functional interface. For some methods that are unique in MySQL but do not exist in other databases or are different in nature, such as the pconnect method of MySQL that may be used in PHP, the same concept as this method does not exist in other databases, so this method should not appear in this basic interface, so what basic methods should there be in this basic interface? PDO already told you.
PDO is an abstract database interface layer, which tells us what basic methods a basic database operation interface should implement. The interface is a high-level abstraction, so the methods in the interface should be universal, basic, and immutable.
There is also a question, how should those unique methods be implemented? According to the ISP principle, these methods can exist in another interface, so that this "alien" can implement both interfaces at the same time.
For interface contamination, you can consider these two treatment methods:
Use delegates to separate interfaces.
Use multi-inheritance to separate interfaces.
In the delegate pattern, two objects participate in processing the same request, and the object that accepts the request delegates the request to another object to process, such as policy pattern, proxy pattern and so on.
Let's take a look at the example again.
Have you ever encountered a very "fat" interface?
For example, there is an interface related to animals, the code is as follows:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.