Article View: borland.public.jbuilder.java.api
Article #2031Re: When to use interfaces in OOP
From: Joi Ellis [TeamB]
Date: Wed, 27 Jun 2001 23:04
Date: Wed, 27 Jun 2001 23:04
53 lines
2762 bytes
2762 bytes
Michael Rachow <michael@famrachow.de> wrote:
> Hi Dick
[snip]
> Only if you let all your different classes implement an Interface that
> requirers that method
> to be defined you can throw away all these instanceof staments and cast all
> these
> different instances to that interface and call that method.
> You can even store now all these instances under that interface and have not
> to cast anymore.
> Ok
Yes. One common example of this is the Runnable interface. Classes
which implement this interface provide a run() method. This allows
any Runnable to be passed into Thread's constructor and turned into
a thread, no matter what parent class those objects extend.
It even allows you to define an anonymous class as a Runnable, something
like this:
public void actionPerformed( ActionEvent ae ) {
Runnable runnableFoo = new Runnable(
public void run() {
...
// do much work here
...
}
);
...
Thread serverReader = new Thread( runnableFoo, "Runnable Foo" );
serverReader.start();
}
I have something similar to this in one of the classes I wrote this
morning. The runnableFoo is a long-lived process that opens a socket
to a remote machine and chats with it a while. The actionPerformed()
method is called from a JMenuItem. By starting placing all the
long-lived work inside a Runnable, I can pass just that work into a
thread of its own. This gets the work off the Swing/Awt Event thread
so that my GUI doesn't freeze and stop painting until the long-lived
thing finishes. Which is exactly what it did when I had the body
of runnableFoo within the actionPerformed() method.
--
Joi Ellis (TeamB) http://www.teamb.com/
No direct email, please.
For best results, post on news server newsgroups.borland.com!
Newsgroup Info: http://www.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<3b3a6688_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mailReferences:
<3b39c705_1@dnews> <3b39d697$1_1@dnews> <3b39ea6d_1@dnews> <3b3a4478_2@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,a43ea4662650fd72
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-06-27 16:12:20 PST
Message-ID: <3b3a6688_1@dnews>
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Subject: Re: When to use interfaces in OOP
Newsgroups: borland.public.jbuilder.java.api
References: <3b39c705_1@dnews> <3b39d697$1_1@dnews> <3b39ea6d_1@dnews> <3b3a4478_2@dnews>
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.16-3 (i686))
NNTP-Posting-Host: 209.46.41.66
Date: 27 Jun 2001 16:04:40 -0700
X-Trace: dnews 993683080 209.46.41.66 (27 Jun 2001 16:04:40 -0700)
Lines: 53
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews!not-for-mail