liveBook · Manning

Interface

As explained in The Art of Code

An interface is a contract that specifies operations a component provides without prescribing their implementation.

An interface defines the operations a component provides while hiding how those operations work. It separates a high-level story from interchangeable implementations. section 3.2.4

Separate report formats from report generation

A format-based conditional grows whenever PDF, CSV, or Excel support changes. A ReportGenerator interface lets each format implement generate(data), so client code can call report.generate(data) without knowing the implementation. section 3.2.4

Use an interface when you need to:

  • Hide low-level implementation behind a stable operation. section 3.2.4
  • Encapsulate interchangeable behaviors in separate classes. section 3.2.4
  • Keep orchestration code simple and make implementations easy to locate. section 3.2.4
Listing 3.6 Decoupling the story with polymorphism
public interface ReportGenerator {     #1
  void generate(List<Data> data);
}

public class PdfReportGenerator implements ReportGenerator {
  public void generate(List<Data> data) {     #2
    //Generating PDF report…
  }
}

Browse the Java definition and its PDF implementation to see the contract-and-implementation relationship directly. section 3.2.4

How The Art of Code covers it

How deep it goes
Demonstrates the contract, polymorphic implementation, and simplified client call, but does not explore interface language rules.
Examples
Uses PDF, CSV, and Excel report generators, contrasting conditional dispatch with ReportGenerator implementations.
What it assumes
Assumes familiarity with components and implementations while demonstrating the abstraction from the report example.
What you can do after
Apply via polymorphism to separate report-format implementations from orchestration code.
How it connects
Connects interfaces with abstraction, hiding, polymorphism, and the Strategy Pattern.

Also explained in

Compare how 7 books explain interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.