SICP Exercise 2.76
Question
As a large system with generic operations evolves, new types of data objects or new operations may be needed. For each of the three strategies—generic operations with explicit dispatch, data-directed style, and message-passing-style—describe the changes that must be made to a system in order to add new types or new operations. Which organisation would be most appropriate for a system in which new types must often be added? Which would be most appropriate for a system in which new operations must often be added?
Answer
First of all, I think we can all agree that the explicit dispatch style is not going to be preferable to the two others for any situation. This style makes it impossible to write truly generic interface procedures. As soon as any new representation is added, every single selector must be updated to handle it, which is cumbersome. It also opens the door to naming conflicts if two tags happen to have the same name.
So the choice is between data-directed and message passing.
It seems that message passing will be useful for a scenario in which new data types must often be added, but not new representations. Since every data type immediately specifies all its selectors, none of the existing functions need to be touched if a new representation is added. But if a new procedure is to be added, every single existing object must be modified to include the new functionality.
In this other scenario, a data-directed approach is more useful. In this, approach, an installer is specified which encapsulates all selectors. This is not technically better or worse than message passing for adding new representations, since it also requires specifying selectors for all possible functions. However, there is one advantage for adding new functionalities: existing functions don’t need to be modified. Rather, new functions need to be added. This is not really more efficient, but contributes to encapsulation and is generally a good practice.