Interface class vs inheritance

218 views
Skip to first unread message

Paul Hansen

unread,
Dec 5, 2011, 1:17:11 PM12/5/11
to Google C++ Mocking Framework
Hi

Gmock encourages you to use interface (pure virtual) classes and
dependency injection.

So you will end up with a lot of interface classes that only have one
implementation (if you do not include the mock class). I suppose
nearly all classes need to have an interface (since nearly all classes
may need to be mocked out). Please comment because somehow it is
against my conviction to create interfaces when there is only one
implementation of the interface in the production code...

Instead of making interfaces, wouldn't it be possible to just let the
mock class inherit from the real class? A downside is that you will
have to make all your methods in your real class virtual and that
gives you a performance penalty...

Thank you very much
Paul

Greg Miller

unread,
Dec 5, 2011, 1:39:35 PM12/5/11
to Paul Hansen, Google C++ Mocking Framework
Hi, Paul.

On Mon, Dec 5, 2011 at 1:17 PM, Paul Hansen <paul.han...@gmail.com> wrote:
Hi

Gmock encourages you to use interface (pure virtual) classes and
dependency injection.

So you will end up with a lot of interface classes that only have one
implementation (if you do not include the mock class). I suppose
nearly all classes need to have an interface (since nearly all classes
may need to be mocked out). Please comment because somehow it is
against my conviction to create interfaces when there is only one
implementation of the interface in the production code...

There's no single right answer. Much of what you mention is really about object-oriented design philosophy and extends far beyond gMock. GMock encourages many widely accepted OO best practices, such as dependency injection and clean interfaces, but it by no means requires them.
 

Instead of making interfaces, wouldn't it be possible to just let the
mock class inherit from the real class?

Yes, gMock can work with that.

G~
Reply all
Reply to author
Forward
0 new messages