Appendix C. Encapsulating Primitives

Encapsulation in object-oriented programming is often interpreted as making sure classes do not expose their implementation. You can go beyond this interpretation and encapsulate all the concepts in your program. Encapsulating implementations in abstract data types provides benefits in code readability, testability, and quality.1

Encapsulating Primitives in Abstract Data Types

Let’s start with a typical method.

double getDiscount(double orderTotal) {}

With encapsulation, we are not concerned with the implementation of getDiscount(), only its interface: the signature of the method. Looking just at the signature, it’s easy to envision what the method does. It receives the total for an order and returns the discount. ...

Get Essential Skills for the Agile Developer: A Guide to Better Programming and Design 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.