- Containment是一种比较简单的复用方法,如果Component B复用Component A,Component B实际上是Component A的一个客户,Component B向客户提供的Component A的功能实际上是Component B直接调用Component A完成的。当然Component B可以扩充Component A的功能。Component B可以直接使用已经存在的Component A,而不需要对Component A做任何改动。
- Containment的例子实现略(潘爱民《COM原理与应用》第四章)
- Aggregation则比较复杂,Component A必须能够适应被Aggregation下的特殊处理。其核心在于QueryInterface函数。Aggregation涉及到聚合对象和被聚合对象双方的协作,体现了真正意义上的COM复用,而Containment只是客户程序和Component的嵌套,这是Containment和Aggregation的本质区别。
- Aggregation的实现(摘自潘爱民《COM原理与应用》)。
Component A的Code
class INondelegatingUnknown
{
public:
virtual HRESULT __stdcall NondelegationQueryInterface(const IID& iid, void **ppv) = 0 ;
virtual ULONG __stdcall NondelegatingAddRef() = 0;
virtual ULONG __stdcall NondelegationRelease() = 0;
};
class CA : public ISomeInterface, public INondelegatingUnknown
{
protected:
ULONG m_Ref;
public:
CA(IUnknown *pUnknownOuter);
~CA();
public :
// Delegating IUnknown
virtual HRESULT __stdcall QueryInterface(const IID& iid, void **ppv) ;
virtual ULONG __stdcall AddRef() ;
virtual ULONG __stdcall Release() ;
// Nondelegating IUnknown
virtual HRESULT __stdcall NondelegationQueryInterface(
COM复用的中Containment(包容)和Aggregation(聚合)的实现
最新推荐文章于 2024-05-20 19:59:35 发布