3

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?

8
  • I think casting and type conversion cover this. Most languages treat casts like an expression, in that the target of the cast is not itself converted, and, the result of the cast expression, to be useful, need to be used (e.g. assigned to another variable). Perhaps you had a more specific dynamic capability in mind (cast to type name provided as string)? This would then also go to reflection.
    – Erik Eidt
    Commented Oct 7, 2020 at 0:18
  • 3
    I find it highly unlikely that COM is based on any Design Patterns at all. In 1992, when COM was introduced, "Design Patterns" were an obscure idea by two crazy Smalltalk programmers who experimented with applying an equally obscure idea from a completely different field. Commented Oct 7, 2020 at 11:55
  • 2
    @JörgWMittag, agreed. It's also worth noting that COM was preceded by work done on OLE, and that from its inception in about 1985, Windows itself employed a somewhat object-oriented paradigm of message-passing. It's not so much based on "design patterns" as we know the term today (and in the sense it applies to programming languages), but on a system design philosophy of componentisation and composition, and COM was basically one of the things Microsoft came up with in than vein.
    – Steve
    Commented Oct 7, 2020 at 12:58
  • Can we say it's based on the QueryInterface design pattern? Or just the interface design pattern? It didn't have to be dynamic, it just happened to work out that way. I expect that most objects don't dynamically change their interfaces. Commented Oct 7, 2020 at 16:19
  • Taking the question at the end of the main body, rather than the one in the title, I think this is an interesting question. I think your mention of "Adapter" is relevant, since what you get back is not an interface, but an adapter that implements that interface. But whether anyone's come up with a name and set of best practices for "objects that know about their own adapters", I'm not sure.
    – IMSoP
    Commented Oct 7, 2020 at 16:59

2 Answers 2

6

Yes, there is a name for it, and I am sure you already know it: it is called Reflection, and was mentioned as an architectural pattern in the book Pattern Oriented Software Architecture #4.

Of course, the term "Reflection" has a broader meaning than just the availability of a queryInterface interface method. It covers systems which provide all kinds of dynamic type introspection. "Querying an object for a representation of itself as a given type" is just one possible way of implementing a specific introspection feature. So "introspection" may also be a suitable term.

I would not go that far to say "COM is based on this pattern". This is one property of COM (among others), surely an important one, nethertheless not the only or primary one.

3
  • Thanks! I've edited the title of the question to more accurately describe that I'm looking for just a design pattern that describes this particular aspect of the COM programming model.
    – Dathan
    Commented Oct 8, 2020 at 4:41
  • In languages I'm familiar with, though, reflection is provided as a facility by the type system, and the language doesn't provide the object instance being reflected upon with an opportunity to influene the reflection in any way. TBH, I've only ever encountered it in that context -- though Ruby being so malleable would probably let you do monkey-patch the reflection API to allow the object to participate dynamically -- and it didn't occur to me that reflection would include this use case.
    – Dathan
    Commented Oct 8, 2020 at 4:44
  • @Dathan: you could also call this a form of "introspection" (see my edit), which is included in "reflection". But I have never seen "introspection" as an "official" pattern name.
    – Doc Brown
    Commented Oct 8, 2020 at 12:53
3

COM's QueryInterface is a library-level implementation of dynamic (checked) casting.

There is no design pattern describing this because design patterns are typically solutions for problems that do not have a trivial language-level solution. Dynamic casting, however, has a trivial language-level solution in most languages.

However, given that different languages support different features, the line between language features and design patterns can be blurry. For example, the Command pattern is pretty much a library-level implementation of first-class closures. More concretely, IIRC Scala has a language-level implementation of Singleton.

This means that the first sentence of this answer is a sufficient description of the functionality, even though you probably won't find "Dynamic Cast" in any list of design patterns.

OTOH, the mechanism enabled by QueryInterface, and the way it is typically used, is pretty much the "Capability Pattern" described by this article: https://dzone.com/articles/the-capability-pattern-future-

1
  • 2
    I think one might say "queryInterface" is a language agnostic version of dynamic casting, since COM components are not bound to a specific host language.
    – Doc Brown
    Commented Oct 8, 2020 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.