Advice from the GoF: Encapsulate the concept that varies

Listen to this post
Subscribe

The Gang of Four book states,

Consider what should be variable in your design and encapsulate the concept that varies.

To a modern agile developer, the word “should” is a bit of a red flag. It presumes that we know, ahead of time, what is going to change. We don’t believe this and we certainly don’t want to depend on guessing correctly.

It is good to remember that the book precedes the agile movement and, in this regard, perhaps reflects an old point of view. But the advice is not obsolete, it just needs a bit of an update. It should say,

Consider what has become variable in your design and encapsulate the concept that varies.

We don’t predict; we react. But what does it mean to encapsulate a concept that varies?

Many things can vary in software such as behavior, the cardinality of relationships, the sequence/workflow of an interaction, the specific design of a subsystem, and the way objects are created and/or stored.

Encapsulation does not simply mean “making things private.” Instead, it means hiding of any kind, of anything. What you hide, you can change. So, the advice really means “hide all variations.”

As we examine the patterns, we will see that each one hides different variations from the rest of the system.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.