search
HomeBackend DevelopmentPHP TutorialWhat is the interface isolation principle?

What is the interface isolation principle?

Apr 15, 2019 pm 03:46 PM
Interface isolation principle

The client should not rely on interfaces it does not need; the dependence of one class on another class should be based on the smallest interface.

It is better to use multiple specialized interfaces than a single general interface.

The dependence of one class on another class should be based on the smallest interface.

An interface represents a role, and different roles should not be assigned to one interface. Interfaces that are not related are merged together to form a large and bloated interface, which is a pollution to roles and interfaces.

"Clients should not be forced to rely on methods they do not use. The interface belongs to the client, not the class hierarchy in which it is located." This is very clear. To put it more generally, do not force clients to use methods they do not use. methods, if users are forced to use methods they do not use, then these customers will face changes caused by changes in these methods they do not use.

What is the interface isolation principle?

What this picture means is: Class A depends on method 1, method 2, and method 3 in interface I, and class B is the implementation of class A's dependence. Class C depends on method 1, method 4, and method 5 in interface I. Class D is the implementation of dependence on class C. For classes B and D, although they both have unused methods (that is, the methods marked in red font in the figure), since interface I is implemented, these unused methods must also be implemented. It can be seen that if the interface is too bloated, as long as the methods appear in the interface, regardless of whether they are useful to the classes that depend on it, these methods must be implemented in the implementation class. This is obviously not a good design. If this design is modified to comply with the interface isolation principle, interface I must be split. Here we split the original interface I into three interfaces. The split design is as shown in the figure

What is the interface isolation principle?

The meaning of the interface isolation principle is: establish a single interface , do not build a huge and bloated interface, try to refine the interface, and try to have as few methods in the interface as possible. In other words, we need to establish dedicated interfaces for each class, rather than trying to build a huge interface for all classes that rely on it to call. In the example in this article, the interface isolation principle is used to change a huge interface into three dedicated interfaces. In programming, it is more flexible to rely on several dedicated interfaces than to rely on one comprehensive interface. Interfaces are "contracts" set externally during design. By decentrally defining multiple interfaces, we can prevent the spread of external changes and improve the flexibility and maintainability of the system.

Speaking of this, many people will think that the interface isolation principle is very similar to the previous single responsibility principle, but it is not. First, the single responsibility principle originally focused on responsibilities; while the interface isolation principle focused on the isolation of interface dependencies. Secondly, the single responsibility principle mainly constrains classes, followed by interfaces and methods, and it targets the implementation and details of the program; while the interface isolation principle mainly constrains interfaces, mainly for abstraction, and for the construction of the overall framework of the program.

When using the interface isolation principle to constrain interfaces, pay attention to the following points:

The interface should be as small as possible, but within limits. It is a fact that refining the interface can improve programming flexibility, but if it is too small, it will cause too many interfaces and complicate the design. So it must be done in moderation.

Customize services for classes that rely on interfaces, exposing only the methods it needs to the calling class, and hiding the methods it doesn't need. Only by focusing on providing customized services for a module can minimal dependencies be established.

Improve cohesion and reduce external interaction. Make the interface use the fewest methods to accomplish the most things.

The principle of interface isolation must be used in moderation. It is not good to design the interface too large or too small. When designing interfaces, only by spending more time thinking and planning can you accurately implement this principle.

The above is the detailed content of What is the interface isolation principle?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:hcoder. If there is any infringement, please contact admin@php.cn delete
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft