An example class diagram

For the class diagram, let's imagine that we have a system that has to poll a queue for messages. Of course, this queue might not always have anything to offer, so it would return nulls. Instead of checking for null, we could simply return special null objects that will have empty behavior. Let's show these message classes in a diagram:

Using the classes from the preceding diagram, whenever there is no number to print, we will return a NullMessage object with empty behavior. In some cases, for optimization purposes, people could have the NullMessage as a singleton instance.

Get Scala Design Patterns - Second Edition 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.