A class should not depend on its users
From CSSEMediaWiki
(Difference between revisions)
(New page: Riel's Heuristic 2.2 This heuristic encourages reusability of classes. If a class needs to use another class, obviously it depends on that class. However the dependency relationship shoul...) |
(→See also) |
||
Line 11: | Line 11: | ||
== See also == | == See also == | ||
* [[Riel's heuristics]] | * [[Riel's heuristics]] | ||
+ | * [[Acyclic dependencies principle]] |
Revision as of 04:32, 6 October 2008
Riel's Heuristic 2.2
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.