Saturday, March 12, 2011

Minimize ripple effects

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)
All of these techniques accomplish the same thing - they confine knowledge of implementation details to the smallest possible part of a program. That is, they keep a secret of some sort.

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

Chitika