A class should not depend on its users
From CSSEMediaWiki
(Difference between revisions)
(→See also) |
m |
||
Line 1: | Line 1: | ||
− | Riel's Heuristic 2.2 | + | :''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. | 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. | ||
Line 12: | Line 12: | ||
* [[Riel's heuristics]] | * [[Riel's heuristics]] | ||
* [[Acyclic dependencies principle]] | * [[Acyclic dependencies principle]] | ||
+ | |||
+ | [[Category:Riel's heuristics]] |
Revision as of 23:26, 20 July 2009
- 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.