Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Interface design question

Reply
Thread Tools

Interface design question

 
 
bvssatish@gmail.com
Guest
Posts: n/a
 
      10-11-2012
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.

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.
 
Reply With Quote
 
 
 
 
Pavel
Guest
Posts: n/a
 
      10-12-2012
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      10-12-2012
On Oct 11, 9:21*pm, (E-Mail Removed) wrote:

> * * * 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.
>
> * * * * 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;
> * * * * };



leaving aside the oddity of deriving from IP4Connection...

wouldn't any derived class constructor call the base class
constructor?

class LemonIPConnection: public IP4Connection
{
public:
LemonIPConnection (const strin& ip, uint port):
IP4Connection (ip, port)
{}
}
 
Reply With Quote
 
Dengjie Li
Guest
Posts: n/a
 
      10-19-2012
i think that we don't design some data members in a abstract class.
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      10-19-2012
On Thursday, 11 October 2012 23:21:12 UTC+3, (E-Mail Removed) wrote:
> Interfaces using abstract classes impose restriction(s) on it's derived implementations.
>
> - The most widely used restriction is to IMPLEMENT Function(s)


Interface is not blueprint of a class, but blueprint of ability of class. It is
a way for object to communicate with other object. Interface declares the
inputs and outputs that are needed for such communication. That is wrong to
think of it as restriction.


> 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.


Generally an interface should not have data members at all. It should be as
light as possible. It is bad to be intrusive. What if the implementer can reach the data from elsewhere without storing it locally?

> 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.


Counter example: Why std::string? Are you certain that it is the best container to store IP address for all possible objects that implement the interface? What if the implementation wraps some operating system's socket resource that already contains the data? Your interface then enforces the data tobe present two times.

> class IP4Connection
> {
> public:
> explicit IP4Connection( string ip, uint port);


explicit is useless keyword here. Compilers should warn that you got worthless 'explicit' in code but for some reason they do not.

> virtual int send() = 0;
> virtual int listen() = 0;
> virtual int reconnect() = 0;
> protected:
> std::string ip;
> uint port;
> };
>
> Thanks,


NP, hope it helps.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic user interface design question MK ASP .Net 0 11-30-2006 06:19 PM
Interface Design Question Michael De Tomaso HTML 3 03-17-2005 06:32 AM
interface design question Rajarshi Guha Java 6 12-09-2004 10:05 PM
Interface design question Martha Goys Java 5 10-28-2004 02:54 AM
PCI interface or USB interface David Wireless Networking 4 09-16-2004 01:01 PM



Advertisments