Skip to content

Expression Problem

Dynamic polymorphism has a fundamental design challenge: Do you design for the ability to easily introduce new types to the system, or do you design for allowing easily adding new operations to existing types?

There is often a trade-off in design whether introducing new types is easy, but adding new operations is difficult, and vice-versa. In OOP, inheritance hierarchies make addition of new operations difficult, because this change intrudes on all existing types. In procedural or functional programming, addition of operations is easier, but introducing new types is difficult, because it intrudes on the existing operations.

If you are able to predict which axis is more likely to require extensibility, choose the appropriate design to accommodate for the future extensions.

  • Be aware if you are more likely to require an Open set of types, Closed set of operations or Open set of operations, Closed set of types
  • Use Visitor Pattern to design for Open set of operations, Closed set of types
  • Use classic Object Oriented design for Open set of types, Closed set of operations
  • Static polymorphism enables extensibility on both axes (types and operations)