A class should not depend on its users

From CSSEMediaWiki
Jump to: navigation, search
Users of a class must be dependent on its public interface, but a class should not be dependent on its users. --Riel's Heuristic 2.2, Arthur Riel 1996

This heuristic encourages reusability of classes. If a class needs to use another class, obviously it depends on that class. However the dependency relationship should only go one way - the user of the class depends on the class, but the class does not depend on the user. This allows a class to be reused in a variety of contexts.

For example, consider a Person class which uses a Toaster class. The Person class is dependent on the public interface of the Toaster class. The Toaster class should not be dependent on the Person class, because if it were, the Toaster could not later be reused as part of a smart home (which could tell the Toaster to start toasting automatically) without attaching a Person to the Toaster.


2.2 example.png

Automatability

This rule can be automated in the same way as the Acyclic dependencies principle, looking for length 2 cycles in the directed graph of class coupling.

See also

Personal tools