Skip to Main Content
Prefactoring
book

Prefactoring

by Ken Pugh
September 2005
Intermediate to advanced content levelIntermediate to advanced
240 pages
6h 28m
English
O'Reilly Media, Inc.
Content preview from Prefactoring

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Understanding Unittest.Mock

Understanding Unittest.Mock

Mario Corchero
Java™ Performance

Java™ Performance

Charlie Hunt, Binu John

Publisher Resources

ISBN: 0596008740Supplemental ContentCatalog PageErrata