OOPortal OOPortal


COM Aggregation   «Prev  Next»
Lesson 2 What aggregation does
Objective Describe aggregation's mechanisms and how they work.

What aggregation does in MS COM

Aggregation provides a mechanism that allows you compose several COM objects into one larger composite COM object. Unlike containment/delegation, where we have a multilevel client/server relationship (that is, the outer COM object acts like a client to the inner COM object), aggregated objects combine with the outer COM object (the aggregator) into what appears to be one COM object that supports the interfaces implemented in all the objects. The following diagram illustrates an aggregation of two COM objects.

Aggregation of two COM objects consisting of 1) Outer COM Object 2) Inner COM Object
Aggregation of two COM objects consisting of 1) Outer COM Object 2) Inner COM Object

Notice how the inner object's interfaces are directly exposed as if they are part of the outer object. A client holding a pointer to an inner object's interface has a pointer directly into the inner object. Yet the client thinks the interface pointer belongs to the outer object
Using aggregation, we can create compositions of several COM objects (not just two). For example, suppose we have components called FindFile, ReadWriteFile, and ArchiveFile. FindFile is an aggregation of two components: LocalFindFile and RemoteFindFile. We can create a FileManager component that reuses the interfaces provided by each component. The following diagram depicts this aggregation of objects.

FileManager component that reuses the interfaces provided by each component.
FileManager component that reuses the interfaces provided by each component.

In the picture above, FileManager supports eight interfaces. Besides its IUnknown (depicted at the top), it gets three interfaces from FindFile, one from ReadWriteFile, two from ArchiveFile, and one of its own interfaces (depicted on the bottom left). Additionally, FindFile is an aggregate with three interfaces: one of its own, one from LocalFindFile, and one from RemoteFindFile.
A client using the FileManager component sees FileManager as implementing all these interfaces and it does not see the FindFile, ReadWriteFile, or ArchiveFile objects. The objects aggregated by FindFile, LocalFindFile and RemoteFindFile are not visible to FileManager either.