liveBook · Manning

Encapsulation

As explained in Functional Design and Architecture: Examples in Haskell

Encapsulation is a design and language mechanism that hides an entity’s internal representation while exposing a controlled interface.

Encapsulation hides internal state behind a controlled interface, preventing casual access and establishing a code contract. A consistent interface protects clients from representation changes, while a poorly designed interface can still invalidate state. section 3.2.4

The need becomes especially clear with mutable state in concurrent code: concentrating changes inside an object limits the impact of later fixes, although encapsulation alone cannot prevent every unwanted mutation. section 3.2.4

Hiding an abstract value representation

A functional module can keep Value’s constructors private while exposing smart constructors such as boolValue, stringValue, and intValue. Clients depend on the interface rather than pattern-matching over BoolValue, IntValue, FloatValue, or StringValue, so representation changes remain localized. section 3.2.4

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains interface contracts, concurrency motivation, smart constructors, and module exports, while deferring detailed concurrency struggles.
Examples
Uses mutable concurrent variables, a Value algebraic data type, smart constructors, and a Haskell module export list.
What it assumes
Assumes familiarity with classes and functional modules while teaching the contract rationale from the problem of exposed state.
What you can do after
Apply smart constructors and restricted module exports to keep clients independent of unstable internal representations.
How it connects
Connects object-oriented programming with abstraction, polymorphism, inheritance, modules, abstract data types, and concurrency.

Also explained in

Compare how 14 books explain encapsulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.