Re: Adding abstract class...
From: christopher diggins (cdiggins_at_videotron.ca)
Date: 04/28/04
- Next message: Daniel T.: "Re: Aggregation vs composition"
- Previous message: Pradyumn Sharma: "Re: attribute or subclass"
- In reply to: Robert C. Martin: "Re: Adding abstract class..."
- Next in thread: Universe: "Re: Adding abstract class..."
- Reply: Universe: "Re: Adding abstract class..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 28 Apr 2004 07:37:39 -0400
"Robert C. Martin" <unclebob@objectmentor.com> wrote in message
news:m4ll801gti09jh4h9tgf3qrr5uon0h49io@4ax.com...
> On Sat, 24 Apr 2004 17:47:12 GMT, "bobsled" <sleding@sands.com> wrote:
>
> >Here's a post from Uncle Bob...
> >
> >"In C++ dependencies are very expensive, so isolation is that much more
> >important. Therefore I think it is often best to keep the interface
> >and add the abstract class below it. In Java or C# the dependencies
> >are not quite as expensive, and need for isolation not quite as great.
> >However, since abstract classes cannot be multiply inherited, it's
> >still probably best to keep the interface and add the abstract class
> >below it.
> >
> >On the other hand, if you started with an abstract class, and did not
> >have an interface; and if the system was working well without the
> >interface, I'm not sure I'd add it right away. I'd wait until some
> >event occurred that made the need for the interface obvious."
> >
> >I have some question and want to ask him:
> >
> >1. Is the difference between interface and abstract base class is the
former
> >consists only pure virtual functions but no member of data and no
> >non-pure-virtual function, and the latter has at least one pure virtual
> >function and either some members of data or non-pure-virtual function?
>
> Yes.
Strictly speaking this is not correct, an abstract base class by definition
doesn't have to have data members, or non-pure virtual functions. According
to the C++ Programming Language by Stroustrup Chapter 12.3: "A class with
one or more pure virtual functions is an abstract class, and no objects of
that abstract class can be created", further on he says also : "An abstract
class can be used only as an interface and as a base for other classes".
> >2. Is keeping adding such kind of abstraction into class hierarchy not
going
> >to gain performance penalty?
>
> No, there's no performance penalty. In C++ virtual deployment is very
> fast; and probably faster than the 'if' statement you'd need to
> achieve the same behavior without it.
It would be more correct to say there is a negligible performance penalty in
many cases. Adding abstract base classes into a hierarchy (compared to
correct implementation of interfaces) does have an execution time penalty
along with object size bloat which I demonstrate at
http://www.heron-language.com/article-cpp-interfaces.html which shows an
interface based design compared with an ABC that performs at 2x-4x as fast,
and reduces the size of the objects by half. The trade-off is that it uses
double width pointers as interface references. (note to Uncle Bob and those
familiar with my work: this links to my most recent work, which is IMHO more
convincing than my previous posts).
> >3. Is there any general rule for such kind of adding abstract class?
>
> There are many rules regarding inheritance, polymorphism, and abstract
> classes. See:
>
http://www.objectmentor.com/resources/listArticles?key=topic&topic=Design%20Principles
-- Christopher Diggins http://www.cdiggins.com http://www.heron-language.com
- Next message: Daniel T.: "Re: Aggregation vs composition"
- Previous message: Pradyumn Sharma: "Re: attribute or subclass"
- In reply to: Robert C. Martin: "Re: Adding abstract class..."
- Next in thread: Universe: "Re: Adding abstract class..."
- Reply: Universe: "Re: Adding abstract class..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|