Interfaces as types

Interfaces specify only what a class can do (or which contracts it obeys) and not how the class does this. On the other hand, non-related classes can share some common behavior. If we somehow could write this behavior in one place, we would be following the Don't Repeat Yourself principle (DRY).

Note

The DRY principle aims at reducing repetition. We can look at a method, a function, or a class as a piece of knowledge. This means that a function or method knows how to do something. The principle tells us not to duplicate this know-how.

It is easier to change this one piece or replace it with another, as this won't affect other logically unrelated elements. Brian Kernighan, the legendary programmer, calls this the Single Point Of ...

Get Scala Functional Programming Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.