What design pattern best describes COM’s queryInterface pattern?


In COM programming, each COM object exposes a queryInterface method. You provide it a type that you want to “cast” the COM object as, and if it supports it it’ll return you a reference to its implementation of the requested type.

It’s sort of like a service registry, but there’s a pseudo-semantic that all the interfaces you can cast a given object to are interfaces to the same object, rather than a constellation of related and linked instances like you’d normally have in a service locator.

In Java I could have a class implement multiple interfaces and just use the language to cast back and forth. But I’m interested in an object being able to decide dynamically whether it can provide an interface or adapter of the requested type, rather than having that decided statically.

Is there a name for this design pattern where you query an object for a representation of itself as a given type?