Much of object programming is centered on minimizing the ripple effects caused by changes to a program. This is done simply by keeping details secret (information hiding or encapsulation).
The principal ways of doing this are
- indirection - named constants replacing "magic numbers", for example
- minimizing visibility - private fields, package-private classes, for example
- generic references (polymorphism) - using high level references (interfaces or abstract classes) instead of low level references (concrete classes)
Constant and liberal use of the above techniques is recommended.
An interesting quote from chapter one of Design Patterns, regarding the use of generic references :
"This so greatly reduces implementation dependencies between subsystems that it leads to the following principle of reusable object-oriented design :
Program to an interface, not an implementation.
Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class. You will find this to be a common theme of the design patterns in this book."
(They state a second principle as well: "Favor object composition over class inheritance.")
No comments:
Post a Comment