随笔-5  评论-3  文章-2  trackbacks-0
一、    创建接口文件.h

#ifndef _DBSERVER_INCLUDE
#define _DBSERVER_INCLUDE

typedef long HRESULT;
#define DEF_EXPORT _declspec(dllexport)

class IDB
{
    //Inte***ces
public:
    //Inte***ce for data access
    virtual HRESULT Read(short nTable,short nRow,LPWSTR lpszData)=0;
    virtual HRESULT Write(short nTable,short nRow,LPCWSTR lpszData)=0;
    //Inte***ce for database management
    virtual HRESULT Create(short &nTable,LPCWSTR lpszName)=0;
    virtual HRESULT Delete(short nTable)=0;
    //Inte***ce for database information
    virtual HRESULT GetNumTables(short &nNumTables)=0;
    virtual HRESULT GetTableName(short nTable,LPWSTR lpszName)=0;
    virtual HRESULT GetNumRows(short nTable,short &nRows)=0;
    virtual ULONG Release()=0;
};

class IDBSrvFactory
{
    //Inte***ce
public:
    virtual HRESULT CreateDB(IDB** ppObject)=0;
    virtual ULONG Release()=0;
};

HRESULT DEF_EXPORT DllGetClassFactoryObject(IDBSrvFactory** ppObject);
#endif

二、    建立对象程序的头文件.h
并进行映射,函数声明顺序和参数跟接口文件一致

#ifndef _DBSERVERIMP_INCLUDE
#define _DBSERVERIMP_INCLUDE
#include "..\Inte***ce\DBsrv.h"

typedef long HRESULT;
//#define DEF_EXPORT _declspec(dllexport)

class CDB:public IDB
{
    //Inte***ces
public:
    //Inte***ce for data access
    HRESULT DEF_EXPORT Read(short nTable,short nRow,LPWSTR lpszData);
    HRESULT DEF_EXPORT Write(short nTable,short nRow,LPCWSTR lpszData);
    //Inte***ce for database management
    HRESULT DEF_EXPORT Create(short &nTable,LPCWSTR lpszName);
    HRESULT DEF_EXPORT Delete(short nTable);
    //Inte***ce for database information
    HRESULT DEF_EXPORT GetNumTables(short &nNumTables);
    HRESULT DEF_EXPORT GetTableName(short nTable,LPWSTR lpszName);
    HRESULT DEF_EXPORT GetNumRows(short nTable,short &nRows);
    ULONG DEF_EXPORT Release();
    //Implementation
private:
    CPtrArray m_arrTables;//Array of pointers to CStringArray(the "database")
    CStringArray m_arrNames;//Array of table names
public:
    ~CDB();
};

class CDBSrvFactory:public IDBSrvFactory
{
    //Inte***ce
public:
    HRESULT DEF_EXPORT CreateDB(IDB** ppObject);
    ULONG DEF_EXPORT Release();
};

HRESULT DEF_EXPORT DllGetClassFactoryObject(CDBSrvFactory** ppObject);
#endif

三、    建立对象的实现文件
参数保持一致

HRESULT CDBSrvFactory::CreateDB(IDB** ppObject)
{
    *ppObject=(IDB*)new CDB;
    return NO_ERROR;
}
HRESULT DEF_EXPORT DllGetClassFactoryObject(IDBSrvFactory** ppObject)
{
    *ppObject=(IDBSrvFactory*)new CDBSrvFactory;
    return NO_ERROR;
}

四、    连接dll
五、    编译
posted on 2006-03-14 19:54 苍羽 阅读(1043) 评论(1)  编辑 收藏 引用 所属分类: 读书笔记

评论:
# re: 抽象基类实现接口 2011-05-13 13:38 | j.y
好文章,找了很久~有原理,有实例...  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理