liveBook · Manning

Interface

As explained in Grokking Software Architecture

An interface is a programming-language construct that defines a contract of operations without prescribing their implementation.

An interface defines a contract of operations without prescribing their implementation. Think of a soccer playbook: a midfielder needs to know how to execute a play, not the details of every possible play. New plays can follow the same contract without rewriting the midfielder’s job. section 3.4.2

Depending on a stable interface rather than concrete details helps keep working code separate from behavior that changes. This supports the Open/Closed Principle: extend functionality by adding implementations instead of repeatedly modifying existing code. Otherwise, growing if/else or switch statements can make changes riskier and harder to maintain. section 3.4.2

Listing 3.6 “After”: Following OCP - Extension
 
// new class for a dribble move
class DribblePastOpponent 
{
     execute()
     {
          console.log(“Executing a dribble move…”);
          // Dribbling logic here
     }
}
 
//new class for a defensive formation
class DefensiveFormation
{
     execute()
     {
          console.log(“Getting into defensive position…”);
          // Defensive logic here
     }
}
 
class Midfielder #A
{
     executePlay(play)
     {
      if (typeof play.execute !== 'function') { #B
             throw new Error("Play must implement an execute() method!");
          }
          play.execute();  
     }
}

Each play provides execute(), while Midfielder checks for that method and calls it. The example demonstrates a shared contract through a runtime check, rather than an explicit interface declaration. section 3.4.2

Keep interfaces focused on the role a client needs. A large, catch-all interface can force classes to implement irrelevant methods, sometimes with errors in place of useful behavior. It also couples clients to changes in responsibilities they do not use. section 3.4.4

Listing 3.10 “After”: Following ISP
class Midfielder {
    practiceShooting() { /* ... */ }
    practiceTackling() { /* ... */ }
}
 
class Goalie {
    practiceShooting() { /* ... */ }
    practiceTackling() { /* ... */ }
    practiceDivingSaves() { /* ... */ }
    practiceHandDistribution() { /* ... */ }
}

The focused alternative lets the midfielder practice shooting and tackling without providing goalie-specific drills; the goalie supplies those additional behaviors. section 3.4.4

In hexagonal architecture, ports are interfaces at the boundary of the core domain, and adapters are their implementations. This boundary helps decouple business logic from external concerns such as databases and user interfaces. section 5.0

How Grokking Software Architecture covers it

How deep it goes
Explains design principles through before-and-after examples, without detailing language-specific interface declarations.
Examples
Contrasts soccer plays with conditional branching and separates midfielder drills from goalie-specific training.
What you can do after
Evaluate interface designs for extension and role focus, identifying unnecessary dependencies and methods.
How it connects
Connects interfaces to the Open/Closed Principle, Interface Segregation Principle, loose coupling, and hexagonal architecture.

Also explained in

Compare how 13 books explain interface →

Related concepts

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