Re: Interface design question
bvssatish@gmail.com wrote:
Hello,
Have a design related question and hope to hear some insights.
Interfaces using abstract classes impose restriction(s) on it's derived implementations.
- The most widely used restriction is to IMPLEMENT Function(s)
Question:
Is it permissible to have similar kind of restriction for any data members in the interface?
In a way that if some one wants to implement the interface it also must initialize certain variable.
Ex:
Say for a TCP/IP it absolutely necessary to have peer's IP address and Port number.
So, is wise to impose a restriction to pass above variable to initialize the class.
I think, yes.
class IP4Connection
{
public:
explicit IP4Connection( string ip, uint port);
virtual int send() = 0;
virtual int listen() = 0;
virtual int reconnect() = 0;
protected:
std::string ip;
uint port;
};
Thanks,
Siva.
I am not sure though why you need the abstract class and virtual methods for
this particular purpose; there is only one "concrete" TCP/IP connection (unless
you want derived classes for IPv4 and IPv6 but even then I would rather had it
as yet another state if I wanted to support both than introduce the hierarchy).
If you decide you do not need abstract class and virtual methods, I would also
make members private (and ended names with _ but it is a matter of style, I
guess :-) ).
-Pavel