design Editor: Martin Fowler, Thought Works ■
[email protected] Protected Variation: The Importance of Being Closed Craig Larman he Pattern Almanac 2000 (Addison- OCP and PV formalize and generalize a Wesley, 2000) lists around 500 soft- common and fundamental design principle ware-related patterns—and given this described in many guises. OCP and PV are reading list, the curious developer has two expressions of the same principle— Tno time to program! Of course, there protection against change to the existing are underlying, simplifying themes and code and design at variation and evolution principles to this pattern plethora that de- points—with minor differences in emphasis. velopers have long considered and dis- I am nominating the term protected varia- cussed. One example is Larry Constantine’s tion for general use, as it is short and clear. coupling and cohesion guidelines In OCP, the term module includes all dis- (see “Structured Design,” IBM crete software elements, including methods, Systems J., vol. 13, no. 2, 1974). classes, subsystems, applications, and so forth. Yet, these principles must contin- Also, the phrase “closed with respect to X” ually resurface to help each new means that clients are not affected if X generation of developers and ar- changes. For example, “The class is closed chitects cut through the apparent with respect to instance field definitions.” PV disparity in myriad design ideas uses the term interface in the broad sense of an and help them see the underlying access view—not exactly a Java or COM in- and unifying forces. terface, for example. One such principle, which Bertrand Meyer describes in Ob- Information hiding is PV, ject-Oriented Software Construction (IEEE not data encapsulation Press, 1988), is the Open–Closed Principle: “On the Criteria To Be Used in Decom- Modules should be both open (for extension posing Systems Into Modules” is a classic and adaptation) and closed (to avoid modifi- that is often cited but seldom read.