应该是第二次看com的书了,总之第一次是糊里糊涂过来的 -_-!! .最大感受:第二次看和第一次感觉有很大不同!其实在《com原理和应用》(记得是这本书吧)开始就有句话:“com是更好的c++”,当时还不觉得,第二次认真看过书后才真正发觉这话说的的却不错!我觉得从不同的角度学习com就会有不同的收获,也会有不同的感受...设计模式,资源的管理等等。这些各位高人都已经有过评价或心中有数,我也不敢在这里妄加评论。总之,我感觉com就象一个熔炉,融合了很多优秀的思想,经典的方法。呵呵,废话了这么多,大家不要骂我,也算我这些天看书的点点收获吧。
   言归正传,看到com连接点部分,自己从设计模式的角度学习了下,仿写了个简单demo,欢迎大家指点。
   主要目的:想从中向各位高手学习设计思路。关注接口,有些具体实现没有写了。如果各位能多多指出这个简单设计上的问题,我真是求之不得了。好!望各位不吝赐教。
    /*
* File : COM_Connetion.h
* simulate Connectionable in COM
* date : 2005.5.19
*/
//efine PRINT(s) printf("illeage param")
typedef unsigned int DWORD ;// 服务器支持的导出Event类 
template <class E_T>
class Event 
{
private:
    DWORD m_dwCookie ;
public:
    bool AddRef()
    {
        // ...
    } 
    bool Release()
    {
        // ...
    }
    bool FireEvent()
    {
        // ...
    }
};
//
// virtual base class BaseConnectionPointor
// Connected server and client , take request from client
// 1.简单起见,只实现了ConnectionPointor而非ConnectionPointorContainer
// 2.each one ConnectionPointor support only one Event 
//
template <class E_T> 
class ConnectionPointor ;template <class E_T> 
class BaseConnectionPointor 
{
public:
    // 客户端调用服务器提供的服务 //
    bool Advice(void **ppSomeEvent, DWORD dwCookie) = 0;
    // 客户端释放出接口 //
    bool Unadvice(DWORD dwCookie) = 0 ;
    // 获取实现该连接点的服务器实例 //
    bool GetServerInterface(void **ppServerPtr) = 0;
    // 支持查询接口  //
    bool QueryInterface(DWORD dwIID, ConnectionPointor<E_T> **ppConnectionPtr) = 0;                          
};
template <class E_T>
class ConnectionPointor : public BaseConnectionPointor<E_T>
{
private:
    Event<E_T> *m_pSomeEvent ;
public:
    // 客户端调用服务器提供的服务 ,Cookie做为每个事件的标识 //
    bool Advice(void **ppSomeEvent, DWORD dwCookie) 
    {
       if(ppSomeEvent==NULL)
        {
            printf("bool Advice(void **ppSomeEvent, DWORD dwCookie) param illeage");
            return false ;
        }
        if(*ppSomeEvent!=NULL)
        {
            printf("bool Advice(void **ppSomeEvent, DWORD dwCookie) param illeage");
            return false ;       
        }
        *ppSomeEvent = m_pSomeEvent;
        m_pSomeEvent->AddRef();
        return true; 
    }
    // 客户端释放出接口 //
    bool Unadvice(DWORD dwCookie) 
    {
        if(0 == m_pSomeEvent->Release())
            m_pSomeEvent = NULL;
        return true;
    }
    // 支持查询接口  //
    bool QueryInterface(DWORD dwIID, ConnectionPointor<E_T> **ppConnectionPtr)      
    {
        // if(dwIID) {}
        if(ppConnectionPtr==NULL)
        {
            printf("bool QueryInterface(DWORD dwIID, **ppConnectionPtr) param illeage");
            return false ;
        }
        if(*ppConnectionPtr!=NULL)
        {
            printf("bool QueryInterface(DWORD dwIID, **ppConnectionPtr) param illeage");
            return false ;       
        }
        *ppConnectionPtr = (ConnectionPointor *)this ;
        return true ;
    }
};
// class Server
class ConnectableServer 
{
    // ...
};
class NonConnectableServer
{
    // ...
};
// 支持出接口的服务器组件需要实现ConnectionPointor类
template <class S_T , class E_T>
class Server  
{
public:
    bool QueryConnetctionPtr(DWORD dwIID, ConnectionPointor<E_T> **ppConnectionPtr)      
    {
        bool ret = m_pConnectionPtr->QueryInterface(dwIID, ppConnectionPtr);      
        if(ret)
            return true;
        else 
            return false;
    }
    // ...
private:
    ConnectableServer *m_pConnectableServer;
    ConnectionPointor<E_T> *m_pConnectionPtr;
};

解决方案 »

  1.   

    同感,写的不错,yes,来个编译的
      

  2.   

    请问楼上两位用的什么编译器??请用c++标准编译器,vc6.0对template支持不好,所以我用的dev-c++编译器.编译是没问题的,这几天有点事,所以没怎么关注,有时间的时候一定好好改下.ps:这个不但可以通过编译,而且可以简单实现connectionpoint功能,呵呵...但是实现有点拙劣,说了1是练手2是抛砖.好,继续关注ing
      

  3.   

    哎呀,偶用的vc6,说实话没试过编译,COM这个东西真要自己编代码写,用mfc的那些永远也不知原理如何