6.1. Honor the Class Maxims

Highly cohesive, loosely coupled classes are a common goal for class design. A cohesive class encapsulates a single concept or idea. A loosely coupled class does not depend on the implementation of other classes.[*]

[*] Larry Constantine created the concepts of coupling and cohesion, and he and Edward Yourdon presented those concepts in Structured Design (Prentice-Hall, 1979). He presents the concepts in relation to object-oriented systems in an article he co-wrote with B. Hendersons-Sellers and I.M. Graham called "Coupling and Cohesion: Towards a Valid Metrics Suite for Object-Oriented Analysis and Design" (Object Oriented Systems, 3:143-158, 1996).

6.1.1. Cohesion

Each class should represent one abstraction. Each class should have a one-line description of the class's purpose and meaning. If you cannot describe a class briefly, it probably represents more than one abstraction. Responsibilities that are added to a class should fit within the description.

The CDDisc class just does not feel right. It appears that the class is mixing two concepts. Looking back through the requirements and use cases, we discover the term Rental. That term seems to be a missing abstraction. Note that a class can represent an event as well as a physical object. The one-line description of a Rental is:

Rental

The loan of a CDDisc for a particular period

Tim and I alter CDDisc so that it has a current Rental if it is rented. Now the class looks like this:

 class CDDisc CDRelease ...

Get Prefactoring now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.