Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "John B. Matthews" Newsgroups: comp.lang.java.programmer Subject: Re: simple method to simulate function pointers in java Date: Tue, 06 Sep 2011 13:39:17 -0400 Organization: The Wasteland Lines: 50 Message-ID: References: <94e54cff-4dab-4c82-ade8-736174705e51@s2g2000vby.googlegroups.com> <4e657653$0$304$14726298@news.sunsite.dk> <39f8a144-5eaa-4b7f-b335-efb75539ff95@glegroupsg2000goo.googlegroups.com> NNTP-Posting-Host: LQJtZWzu+iKlBROuDg+IUg.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X) X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7632 In article <39f8a144-5eaa-4b7f-b335-efb75539ff95@glegroupsg2000goo.googlegroups.com >, Lew wrote: > Arne Vajhøj wrote: > > Lino Ferrentino wrote: > >> Maybe there are other methods... I use this: > >> > >> [code omitted] > >> > >> to call the callback > >> > >> my_callback.f(42); > >> > >> > >> For each function pointer type we create an interface. > > > > It is perfectly valid. > > > > You need to note two things: > > 1) the codes is not following Java coding convention > > 2) it is based on an assumption that all functions with > > the same signature are interchangeable - that is not > > very type safe > > The Java idiom is to use a functor, typically a > single-abstract-method (SAM) interface, and pass an instance of that > functor type to the object needing a callback or similar method. The > object implementing the functor interface is the called-back object. > > And as Arne says, do follow the coding conventions. Your code was > nearly impenetrable. You name things by purpose in Java, not by > implementation type. > > Roughly (very roughly)_speaking, the functor pattern is something like: > > [code omitted] A related example of this widely used pattern may be found in `EventListenerList` [1], which adds a measure of type safety by using a class literal as a runtime type-token [2]. [1] [2] -- John B. Matthews trashgod at gmail dot com