liveBook · Manning

Abstraction

As explained in Grokking Software Architecture

An abstraction is an interface or simplified representation that exposes relevant behavior while hiding implementation details.

Think of a coach’s playbook: it describes the goal and each player’s role without spelling out every physical move. An abstraction does something similar in software. It exposes relevant behavior through a simplified representation or contract while hiding implementation details. section 3.4.5

Interfaces and abstract classes can provide shared contracts. Depending on those contracts instead of concrete implementations lets components change more independently. It can also make testing easier: a real database, for example, can be replaced with a fake for a unit test. section 3.11 section 3.4.5

From specific players to a shared role

A coach that creates a Forward and a Midfielder and calls their specific methods is tied to those players. A coach given a collection of players that each support performAction() can run the plan without knowing each player’s concrete behavior. A new Winger can join without changing the coach’s loop. section 3.4.5

Listing 3.11 “Before”: Violating DIP
// Low-level modules (concrete players)
class Forward { 
    attack() { console.log(“Forward is attacking.”); } 
}
class Midfielder {
    controlMidfield() { 
          console.log(“Midfielder controlling the game.”);
    }
}
 
// High-level module (the Coach)
class Coach { #A
     constructor() {    
         this.forward = new Forward(); #B
         this.midfielder = new Midfielder();#B
     }
     executeGamePlan() {
          this.forward.attack();
          this.midfielder.controlMidfield();
     }
}

The before listing ties the coach directly to specific player classes and methods. section 3.4.5

Listing 3.12 “After”: Following DIP
 
// Low-level modules (depend on the abstraction)
class Forward {
    performAction() {
        console.log(“Forward is attacking.”);
    }
}
 
class Midfielder {
    performAction() { 
        console.log(“Midfielder is controlling the game.”);
    }
}
 
class Winger { #A
    performAction() { 
        console.log(“Winger is running down the sideline.”); #A
    }
} #A
 
// High-level module (also depends on abstractions)
class Coach {
    constructor(players) { this.team = players; } #B
    executeGamePlan() {
          for (const player of this.team) {
               player.performAction();
          }
     }
}

The after listing gives the coach players that share a performAction() contract. section 3.4.5

A useful abstraction keeps the caller focused on the right outcome. For example, checkout logic concerned with payment outcomes is easier to separate from a provider’s raw HTTP status codes and network details. section 10.4.1

But a clean interface can hide expensive work. An ORM can spare a client from writing SQL while concealing performance problems on the server. When a query times out, deadlocks, or returns the wrong data, inspecting generated SQL and understanding indexes, joins, and execution plans can help reveal the problem. Use the abstraction, but learn enough about what it hides to debug it. section 6.8 section 6.8.3

How Grokking Software Architecture covers it

How deep it goes
Explains dependency inversion through before-and-after implementations and highlights hidden performance and debugging costs.
Examples
Uses a coach and player contracts, database fakes, payment integrations, and ORM-generated SQL to illustrate abstraction.
What you can do after
Evaluate and understand where contracts aid substitution and where hidden implementation details may complicate debugging.
How it connects
Connects abstraction with dependency inversion, loose coupling, separation of concerns, and testability.

Also explained in

Compare how 24 books explain abstraction →

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.