NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Senior88 views

Расскажите о принципе разделения интерфейсов (Interface Segregation Principle, ISP).

Practice

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.