Interface design question

Discussion in 'C++' started by bvssatish@gmail.com, Oct 11, 2012.

  1. Guest

    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.
    , Oct 11, 2012
    #1
    1. Advertising

  2. Pavel Guest

    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
    Pavel, Oct 12, 2012
    #2
    1. Advertising

  3. On Oct 11, 9:21 pm, 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)
    {}
    }
    Nick Keighley, Oct 12, 2012
    #3
  4. Dengjie Li Guest

    i think that we don't design some data members in a abstract class.
    Dengjie Li, Oct 19, 2012
    #4
  5. Öö Tiib Guest

    On Thursday, 11 October 2012 23:21:12 UTC+3, 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.
    Öö Tiib, Oct 19, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wildepiet
    Replies:
    0
    Views:
    1,848
    Wildepiet
    Jun 14, 2004
  2. Martha Goys

    Interface design question

    Martha Goys, Oct 26, 2004, in forum: Java
    Replies:
    5
    Views:
    335
    Martha Goys
    Oct 28, 2004
  3. Rajarshi Guha

    interface design question

    Rajarshi Guha, Dec 9, 2004, in forum: Java
    Replies:
    6
    Views:
    362
    Eric Sosman
    Dec 9, 2004
  4. Michael De Tomaso

    Interface Design Question

    Michael De Tomaso, Mar 10, 2005, in forum: HTML
    Replies:
    3
    Views:
    374
    KHaled
    Mar 17, 2005
  5. MK
    Replies:
    0
    Views:
    391
Loading...

Share This Page