8.5 A Useful Abstract Class That Is Not a Base Class?
Recall in Chapter 5 that we discussed Heuristic 5.6, which states, "All abstract classes must be base classes in order to be useful." At that time, we observed that for all intents and purposes this was true, but we mentioned an anomalous case whose discussion we postponed. Consider a class composed of all class-specific data and methods. In our keyword implementation of class-specific data and behavior, this would amount to all static data and function members (a la C++). Such a class could be abstract, given that we would not define a constructor, yet the class would have some use in the system. This technically shows an abstract class from which no one inherits yet it is still useful. Why would anyone desire a class with no object-specific methods or data? I had never seen a need for such a class, but someone had mentioned this example in a design course long ago and I passed it along for the purists to ponder. Robert Martin, in the course of reviewing this book, provided a practical example for such a class wherein it is used to encapsulate and limit the scope for a collection of global data. This allows for better understandability of the application as well as the avoidance of namespace pollution (i.e., the cluttering of an application's global name space with duplicate names, causing link errors). I pass along his example for your review (see Figure 8.5).
Figure 8.5. Example of a useful abstract class that lacks derived classes.