Contained objects should not use each other

From CSSEMediaWiki
Jump to: navigation, search
Objects that share lexical scope—those contained in the same containing class—should not have uses relationships between them. --Riel's Heuristic 4.14, Arthur Riel 1996

In this heuristic objects contained in the same containing class should not have uses relationships between them because of reusability issues primarily and complexity issues secondary.

Riel cites a hypothetical ATM class that contains a card-reader object, a display screen object, and a keypad object. Whenever the card reader has detected a valid card, a typical solution (according to Riel) is for the card reader to ask the display screen to display the prompt and then ask the keypad to get the pin from the user. In terms of reusability this solution is not ideal because assuming that you would want to use the card reader object in a security door, you would then have to include the display screen and keypad objects because the card-reader is dependent on them.

In terms of complexity, the fact that the ATM machine class contains the three objects implies that there are uses relationships between the ATM machine class and the three objects. Having additional uses relationships between the three objects makes the design more complex.

The solution is to have the ATM machine use the card reader to detect detect a valid card, then have the ATM machine use the display screen to display the prompt, and then finally, have the ATM machine use the keypad to get the pin from the user.

Personal tools