Simple Object-Oriented Design: Create clean, maintainable applications cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

5 Designing good abstractions

 

This chapter covers

  • Understanding abstractions
  • Adding abstractions in code
  • Keeping abstractions simple

Good abstractions allow us to add new functionality to a system without constantly changing existing code. For example, think of a bookstore with a range of discounts like “Buy three books, get one free,” “45% off during Christmas,” and “Buy five e-books, get one printed copy free.” The marketing team proposes new discounts regularly, so the development team needs an easy way to add them to the code. A well-designed software system will have abstractions in place so developers can add new discounts with minimal effort.

It’s difficult to define what abstractions are in one sentence, so I’ll use a few:

  • They describe a concept, functionality, or process in a way that clients can understand without knowing the underlying mechanisms.
  • They focus on essential characteristics and ignore nonessential ones.
  • Abstractions don’t care (and don’t know) about their concrete implementations.

Abstractions work well with extension points. An extension point enables developers to extend or modify the system’s functionality. In the bookstore, an extension point would allow developers to plug in or unplug whatever discounts should be applied for a given basket.

5.1 Design abstractions and extension points

5.1.1 Identifying the need for an abstraction

5.1.2 Designing an extension point

5.1.3 Attributes of good abstractions

5.1.4 Learn from your abstractions

5.1.5 Learn about abstractions

5.1.6 Abstractions and coupling

5.1.7 Example: Giving badges to employees

5.2 Generalize important business rules

5.2.1 Separate the concrete data from the generalized business rule

5.2.2 Example: Generalizing the badge rules

5.3 Prefer simple abstractions