Расскажите о принципе разделения интерфейсов (Interface Segregation Principle, ISP).
Asked at
AI answer
The Interface Segregation Principle (ISP) states that a client should not depend on methods it does not use. It is better to have several small interfaces with specific responsibilities than one large, “all-purpose” interface.
For example, if classes need different device capabilities, you should not make each of them implement a single Machine interface with methods for printing, scanning, and faxing. You can split it into Printer, Scanner, and Fax; a simple printer would implement only Printer.
In Python, this is usually expressed with small Protocols or abstract classes:
from typing import Protocol
class Printer(Protocol):
def print_document(self, text: str) -> None: ...
class Scanner(Protocol):
def scan(self) -> bytes: ...
def print_report(device: Printer, report: str) -> None:
device.print_document(report)
This design reduces coupling, makes it easier to substitute dependencies in tests, and allows implementations to be added without unnecessary stubs. It is important not to split interfaces mechanically: they should be separated according to clients' actual needs.
Also asked at getmatch agency
- Why did you leave your previous job?
- What types of data are there in Python?
- Tell us about your work experience.
- A user uploads a video and selects the desired processing, which he wants to get. The service accepts the video, applies the necessary processing, and outputs the final result. By processing, we can understand, for example, the following: make the video black and white, flip it, speed it up N times, etc. It is important that the user can monitor the progress of the processing. Load: 10,000 videos per minute
- What frameworks have you worked with?
Similar questions
- What is the Dependency Inversion Principle (DIP), and do you apply it?
- How does the Dependency Inversion Principle differ from dependency injection?
- Tell me about design patterns: are you familiar with them, what kinds do you know, and which ones do you use?
- Can you give a practical example of applying DIP?
- How to check if the value inside interface{} equals nil (write code)?