接口类如下,iBase中的两个函数在其继承类中的表现无特殊变化,所以,为了不在每一个子类中都重复这两个函数的代码,需要一种方法,将这两个函数加入子类中
1. 继承
2.宏
.
struct
iBase
{
virtual void addref() = 0 ;
virtual void release() = 0 ;
};
struct iConnection : public iBase
{
virtual void open() = 0 ;
virtual void close() = 0 ;
};
strcut iDataAccess : public iBase
{
virtual void read_rows() = 0 ;
virtual void delete_row() = 0 ;
virtual void insert_row() = 0 ;
virtual void modify_row() = 0 ;
virtual void update() = 0 ;
};
{
virtual void addref() = 0 ;
virtual void release() = 0 ;
};
struct iConnection : public iBase
{
virtual void open() = 0 ;
virtual void close() = 0 ;
};
strcut iDataAccess : public iBase
{
virtual void read_rows() = 0 ;
virtual void delete_row() = 0 ;
virtual void insert_row() = 0 ;
virtual void modify_row() = 0 ;
virtual void update() = 0 ;
};
1. 继承
class
implBase :
public
virtual
iBase
{
// common code
};
class implConnection : public virtual iConnection, public implBase
{
// implement interface of iConnection
// no need to implement interface of iBase
};
{
// common code
};
class implConnection : public virtual iConnection, public implBase
{
// implement interface of iConnection
// no need to implement interface of iBase
};
2.宏
#define
COMMON_FUN public: void addref(){} void release(){}
class implBase : public iBase
{
COMMON_FUN
};
class implConnection : public iConnection
{
// implement interface of iConnection
COMMON_FUN // need do this here
};
class implBase : public iBase
{
COMMON_FUN
};
class implConnection : public iConnection
{
// implement interface of iConnection
COMMON_FUN // need do this here
};
.