Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: blmblm@myrealbox.com Newsgroups: comp.lang.java.programmer Subject: Re: generics puzzle Date: 19 Oct 2011 13:25:46 GMT Organization: None Lines: 80 Message-ID: <9g81eqFj6eU1@mid.individual.net> References: <9g2f24Fi0vU1@mid.individual.net> <3euvm8-ee2.ln1@news.simpsonst.f2s.com> X-Trace: individual.net 49oth3c52MWoxhdxPBoa0wOxYSMoYblW2H6x2IQV0odPCbt3KI X-Orig-Path: not-for-mail Cancel-Lock: sha1:CmCKLVVNgiJKvQ/opY7HZuBZCAk= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8990 In article , Daniel Pitts wrote: > On 10/18/11 2:45 AM, Steven Simpson wrote: > > On 17/10/11 16:58, Daniel Pitts wrote: > >> On 10/17/11 5:14 AM, Steven Simpson wrote: > >>> On 17/10/11 11:41, blmblm@myrealbox.com wrote: > >>>> One fix is to just introduce a method setFromModified() in GThing, > >>>> but that doesn't appeal to me. > >>> > >>> Instead of adding it to GThing, create a static method: > >>> > >>> private static void setModified(GThing t) { > >>> t.set(t.modified()); > >>> } > >> > >> > >> What about the simpler solution: > >> > >> in the GThing class: > >> > >> public void setModified() { > >> set(modified()); > >> } > > > > It "doesn't appeal" to the OP? Nor to me. Not sure I can put my finger > > on it, but if I were the maintainer of GThing, I'd consider its > > specification to be complete without this method. It doesn't increase > > the value of the class as an abstraction. Adding it would certainly just > > be a convenience for the caller. It solves a problem generated by the > > design of the call site, not the design of the class's contract. The > > caller can write his own routine as necessary. > > > Possibly, but if it is something that happens frequently, then it seems > more likely to belong to GThing than externally. Otherwise your > call-site is suffering from the code smell "feature envy" > > One other approach is a "visitor" pattern: > I know the "visitor" pattern (vaguely anyway), but .... I'm not sure I understand your version of it here, which -- does this compile? because .... > > public interface GThingVisitor { > void visit(GThing gThing); > }; (Aside: Hm, a generic method in an interface?! well, why not, maybe, but I'm not sure it would have occurred to me to try!) > public class SetModifiedGThingVisitor { > void visit(GThing gThing) { > gThing.set(gThing.modified()); > } > } Was this meant to implement GThingVisitor? > public class GThing { > public void accept(GThingVisitor visitor) { > visitor.accept(this); > } > } visitor.visit(this)?? > public class CallSite { > public void setAllModified(Iterable> things) { > GThingVisitor visitor = new SetModifiedGThingVisitor(); > for (GThing thing: things) { > thing.accept(visitor); > } > } > } > -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.