Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #1552

Re: Interchanging objects?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.help
Subject Re: Interchanging objects?
Date Sun, 5 Feb 2012 11:34:31 -0800 (PST)
Organization http://groups.google.com
Lines 63
Message-ID <8494969.1891.1328470471486.JavaMail.geo-discussion-forums@prnc3> (permalink)
References <bb014e7e-2bf1-4d55-8677-6e2514826da5@p7g2000yqk.googlegroups.com> <2362859.192.1328425259754.JavaMail.geo-discussion-forums@prhb20> <9f687d98-30d7-4838-b09d-5ff6a77139b9@1g2000yqv.googlegroups.com>
Reply-To comp.lang.java.help@googlegroups.com
NNTP-Posting-Host 173.164.137.214
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1328470472 24598 127.0.0.1 (5 Feb 2012 19:34:32 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sun, 5 Feb 2012 19:34:32 +0000 (UTC)
In-Reply-To <9f687d98-30d7-4838-b09d-5ff6a77139b9@1g2000yqv.googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.help:1552

Show key headers only | View raw


Davej wrote:
> I have been trying to think of a convenient way to interchange objects
> that provide calculations that are done slightly differently. The
> objects all have the same method names and return the same types. Is
> there an elegant way to accomplish this? Thanks.

Yes. Instead of thinking in terms of objects, think in terms of types.

You have some concept of a type that will perform certain behaviors, the 
details of which will be handled by different specific subtypes of that type.

So define your overall type as in interface and have your specific subtypes 
implement that interface. Something along the lines of (not complete code):

 public interface Schmoo
 {
   Spronk schmooze(Scronx scronx);
 }

Now let's say you have two different kinds of 'schmooze()' you want types to 
perform.

 public class TankerSchmoo implements Schmoo
 {
   @Override public Spronk schmooze(Scronx scronx)
   {
     // an implementation that somehow involves Tankers
   }
 }

and

 public class HonkerSchmoo implements Schmoo
 {
   @Override public Spronk schmooze(Scronx scronx)
   {
     // an implementation that somehow involves Honkers
   }
 }

Mind you, you cannot convert TankerSchmoo to HonkerSchmoo or vice versa, but 
you can refer to either one as a Schmoo.

 Schmoo aSchmoo = new TankerSchmoo();
 Schmoo anotherSchmoo = new HonkerSchmoo();

You can have a collection of such things.

 List<Schmoo> schmoos = Arrays.asList(aSchmoo, anotherSchmoo);

and 

 Scronx scronx = getScronx();
 for (Schmoo schmoo : schmoos)
 {
   schmoo.schmooze(scronx);
 }

But what you need to do is study the Java tutorials and the conversion rules to 
which I've already alluded, and understand inheritance and polymorphism.

-- 
Lew

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Interchanging objects? Davej <galt_57@hotmail.com> - 2012-02-04 12:02 -0800
  Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-04 23:00 -0800
    Re: Interchanging objects? Davej <galt_57@hotmail.com> - 2012-02-05 07:41 -0800
      Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-05 11:34 -0800
      Re: Interchanging objects? Roedy Green <see_website@mindprod.com.invalid> - 2012-02-08 22:42 -0800
        Re: Interchanging objects? Lew <lewbloch@gmail.com> - 2012-02-09 09:21 -0800
  Re: Interchanging objects? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-02-07 08:09 -0500

csiph-web