6.5. One Little Job

The framework of Unix pipes and filters is elegant. It is easier to create a program that does one simple job (a filter) than a program that does multiple jobs. For example, the word count program (wc) counts lines, words, and characters in the input file. The directory-listing program (ls) lists the files in a directory, one per line. To count how many files are in a directory, you pipe the output of the ls program into the input of the wc program and count the number of lines (e.g., ls | wc).[*]

[*] Unix commands represent another approach to the naming issue. The names are short. (Some people refer to them as cryptic.) Since these commands are typed over and over, the short names are reasonable in that contact.

Each class should capture one concept. Likewise, each method within a class should do one little job well. The Proxy pattern works in this way. Each proxy class can perform one operation (e.g., encryption or checking for security access violations). Once a proxy method has finished its task, it calls the corresponding method in another proxy object to perform further operations.

Multilevel delegation works in a similar manner. The lowest-level class performs simple operations. Higher-level classes perform more complicated operations, which are based on the lower-level operations. The lowest-level classes get used more often, and the higher-level classes are used less often, since they are designed for specific purposes.

For example, here is a File ...

Get Prefactoring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.