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






