The Artima Developer Community
Sponsored Link

Designing with Interfaces, cont.



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Designing with Interfaces, cont.

Posted by Lois Goldthwaite on 03 Dec 1998, 3:39 AM

> I received this e-mail from Lois Goldthwaite. I thought people
> here might be interested in it. I'm posting it with Lois's
> permission. - Bill Venners
Now we see the REAL reason why the future of the web is Java -- HTML gobbles
up all the angle brackets from C++ and spits out the mangled remains!
I've written this message completely in HTML (if the conversion
tools happens to print all the HTML tags out, I'll wonder why
it couldn't do the same for C++).

If the specialization examples didn't make sense to you, there's a good
reason. :-)

I've edited the complete program to replace all the less-than signs with
ampersand-ell-tee-semicolon and all the greater-than signs with
ampersand-gee-tee-semicolon. I hope this makes things clearer. If it doesn't
work, I'll try again with percent-3c and percent-3e, or replace them with
C++ trigraphs (which the compiler is happy with, but are sub-optimal for
human readers).

The exception is the double-less-than signs which follow cout (equivalent to
System.out.print), which seem to appear OK when I test this using both
Netscape and IE. If what you see doesn't follow cout with a series
of less-than-less-than symbols and some strings terminated by a semicolon,
maybe "View Source" will help. Or look at it again in the "Reply" box -- the
symbols seem to show up there.

I'm glad to discover this site -- it looks like some very interesting
discussions take place here.

Here's the program:

// talkativ.cpp

#include < iostream >
using std::cout;
using std::endl;

// some domain objects
class Dog {
void talk() { cout << "woof woof" << endl; }

class CuckooClock {
void talk() { cout << "cuckoo cuckoo" << endl; }

class BigBenClock {
void talk() { cout << "take a long tea-break" << endl; }
void playBongs() { cout << "bing bong bing bong" << endl; }

class SilentClock {
// doesn't talk

// generic template to provide non-inheritance-based polymorphism
template < class T >
class Talkative {
T& t;
Talkative( T& obj ) : t( obj ) { }
void talk() {; }

// specialization to adapt functionality
template < >
class Talkative < BigBenClock > {
BigBenClock& t;
Talkative( BigBenClock& obj ) : t( obj ) { }
void talk() { t.playBongs(); }

// specialization to add missing functionality
template < >
class Talkative < SilentClock > {
SilentClock& t;
Talkative( SilentClock& obj ) : t( obj ) { }
void talk() { cout << "tick tock" << endl; }

// adapter function to simplify syntax in usage
template < class T >
Talkative < T > makeTalkative( T& obj ) {
return Talkative < T > ( obj );

// function to use an object which implements the
// Talkative interface, C++ style
template < class T >
void makeItTalk( Talkative < T > t )

int main()

Dog aDog;
CuckooClock aCuckooClock;
BigBenClock aBigBenClock;
SilentClock aSilentClock;

Talkative < Dog > td( aDog );;

Talkative < CuckooClock > tcc( aCuckooClock );;

makeTalkative( aDog ).talk();
makeTalkative( aCuckooClock ).talk();

makeItTalk( makeTalkative( aBigBenClock ) );
makeItTalk( makeTalkative( aSilentClock ) );

return 0;

The output from this program is:

woof woof
cuckoo cuckoo
woof woof
cuckoo cuckoo
bing bong bing bong
tick tock



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us