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












