How to program to an interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mikael

    #1

    How to program to an interface

    Hi

    I've been wondering about how "programmin g to an interface, not an
    implementation" works in C++.

    As I understand it, I start with an abstract base class as the interface:

    class TestInterface {
    public:
    ...
    virtual int foo() = 0;
    ...
    };

    - and use this for the implementation:

    class TestImp : public TestInterface {
    public:
    ...
    virtual int foo();
    ...
    private:
    ...
    };


    In an client class I may then do something like:

    Client::UseFoo( TestInterface *test) {
    test->foo();
    }

    But that's just polymorphism - so "Interface = Polymorphism"?

    And the test object still needs to be created somewhere, like:

    Client::UseFoo( ) {
    TestImp *test = new TestImp;
    test->foo();
    delete test;
    }

    So the client still needs to know about the implementation. So why use an
    interface if polymorphism is not needed? What if the private part of TestImp
    contained some classes from an external lib, that the client should not know
    about?

    Obviously I'm missing something :)





  • Gianni Mariani

    #2
    Re: How to program to an interface

    Mikael wrote:
    [color=blue]
    >
    > Obviously I'm missing something :)
    >[/color]

    Factories.

    Your header file should contain only the interface.

    Your IMP.cpp file can contain the whole definition of TestImp.

    You can add a factory method e.g.

    TestInterface * MkTest(); // in header

    - now you have complete separation from implementation and interface.

    You can also use generic factories ....


    (the doc is useless - see the test case).

    Comment

    • Mikael

      #3
      Re: How to program to an interface

      "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
      news:88-dnbJf1IP7OpvZnZ 2dneKdnZydnZ2d@ speakeasy.net.. .[color=blue]
      > Factories.
      >
      > Your header file should contain only the interface.
      >
      > Your IMP.cpp file can contain the whole definition of TestImp.
      >
      > You can add a factory method e.g.
      >
      > TestInterface * MkTest(); // in header
      >
      > - now you have complete separation from implementation and interface.
      >
      > You can also use generic factories ....
      >
      > http://austria.sourceforge.net/dox/h...Factories.html
      > (the doc is useless - see the test case).
      >[/color]


      So - to stick with my example - the following is the way to do it (changes
      marked with a "|") ?

      class TestInterface {
      public:
      | TestInterface *Create();
      ...
      virtual int foo() = 0;
      ...
      };

      and in the cpp file for TestInterface:

      | #include "TestInterface. h"
      | #include "TestImp.h"
      | TestInterface:: Create() {
      | return new TestImp();
      | }

      Should Create be static? virtual?

      One nitpick, TestInterface is no longer a pure abstract base class, because
      of Create. Is that a problem?



      Comment

      • Tomás

        #4
        Re: How to program to an interface

        [color=blue]
        > One nitpick, TestInterface is no longer a pure abstract base class,
        > because of Create. Is that a problem?[/color]

        It still has a virtual function that it doesn't define... sounds abstract
        to me.


        -Tomás

        Comment

        • Dervish

          #5
          Re: How to program to an interface

          >But that's just polymorphism - so "Interface = Polymorphism"?
          It seems to me that current idea of interfaces in C++ came from Java,
          where there is a huge difference between interface and class.
          Originally in C++ interface is .h file (class declaration) and
          implementation is cpp file. Nevertheless I can say that "C++ interface
          class" is a class where exist only following:
          1) Pure virtual functions.
          2) Virtual destructor.
          3) Enumerators and/or const static _data_ members.

          So no data members (even static) & no implemented functions.

          Comment

          • Rolf Magnus

            #6
            Re: How to program to an interface

            "Mikael" <none> wrote:
            [color=blue]
            > So - to stick with my example - the following is the way to do it (changes
            > marked with a "|") ?
            >
            > class TestInterface {
            > public:
            > | TestInterface *Create();
            > ...
            > virtual int foo() = 0;
            > ...
            > };
            >
            > and in the cpp file for TestInterface:
            >
            > | #include "TestInterface. h"
            > | #include "TestImp.h"
            > | TestInterface:: Create() {
            > | return new TestImp();
            > | }
            >
            > Should Create be static?[/color]

            Yes.
            [color=blue]
            > virtual?[/color]

            No.
            [color=blue]
            > One nitpick, TestInterface is no longer a pure abstract base class,
            > because of Create.[/color]

            It's still abstract.
            [color=blue]
            > Is that a problem?[/color]

            No. Why would it be?

            Comment

            Working...