Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.programmer > #5550

Re: Passing a Method Name to a Method

From markspace <-@.>
Newsgroups comp.lang.java.programmer
Subject Re: Passing a Method Name to a Method
Date 2011-06-22 14:14 -0700
Organization A noiseless patient Spider
Message-ID <ittm03$u36$1@dont-email.me> (permalink)
References <81h4075t4gfjglji1n033rb20025ebho68@4ax.com>

Show all headers | View raw


On 6/22/2011 12:46 PM, Gene Wirchenko wrote:
> Dear Jav'ers:
>
>       Maybe I will find this after searching more, but so far, no good.
>
>       I want to pass a parameter of a method name to another method.


As others have said, you can't do this literally.  However you have a 
few options using interfaces, classes, dependency injection, and good 
ol' software engineering.

First, the interface/class method:

public class Parser {
   final private Language parser;
   public Parser( Language parser ) {
     this.parser = parser;
   }
   public parse( Reader in ) {
     parser.parse( in );
   }
}

Here, you use a Parser which uses dependency injection to determine 
which language you are going to parse.  You use it like this:

   Parser p = new Parser( new JavaParser() );
   p.parse( System.in );  // parse stdin

Where JavaParser is:

   public class JavaParser implements Language { ...

and

   public interface Language {
     parse( Reader in );
   }

You could of course pass in the language at the same time, but that's a 
little less OOD.

public class Parser {
   public static parse( Language parser, Reader in ) {
     parser.parse( in );
   }
}

Note that the "don't do it in Java" guy, Fuschia, is off his rocker. 
This is very efficient in Java as the JVM will recognize the JavaParser 
class is effectively final and optimize the call to use non-virtual calls.

You can also do similar things with Runnable or Callable, which just 
avoids declaring a type.  Since declaring a type isn't that big of a 
deal, this is usually poor solution unless you are in fact spawning 
threads and using the concurrency API directly.

public class Parser {
   final private Runnable parser;
   public Parser( Runnable parser ) {
     this.parser = parser;
   }
   public parse() {
     parser.run();
   }
}

You might call this with an anonymous class rather than declaring a yet 
another type, however again that might not really be worth it.  I'm 
drawing a blank here how to pass in arguments to the run method, so I'll 
cheat:

   Parser p = new Parser( new Runnable() {
       private final Reader in = System.in;
       public void run() {
         new JavaLanguage.doIt( in );
       }
     } );
   p.parse();  // parse stdin

Note that when you see this for "real", say in the Java API, you'll 
typically use a factory method, not instantiate an injectable object. 
(For example, both the ScriptEngineManager and 
javax.xml.parsers.DocumentBuilder use factory methods.)  So don't ignore 
the Factory Pattern in your own designs as well.

   Parser p = Parser.getParser( "Java" );
   p.parse( System.in );  // parse stdin

No code compiled or tested. ;)

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


Thread

Passing a Method Name to a Method Gene Wirchenko <genew@ocis.net> - 2011-06-22 12:46 -0700
  Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-22 16:00 -0400
    Re: Passing a Method Name to a Method Gene Wirchenko <genew@ocis.net> - 2011-06-23 14:40 -0700
      Re: isIdentifierCharacter (was: Passing a Method Name to a Method) Gene Wirchenko <genew@ocis.net> - 2011-06-23 16:09 -0700
  Re: Passing a Method Name to a Method markspace <-@.> - 2011-06-22 14:14 -0700
    Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-22 22:59 -0400
    Re: Passing a Method Name to a Method blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-23 17:49 +0000
      Re: Passing a Method Name to a Method markspace <-@.> - 2011-06-23 11:22 -0700
        Re: Passing a Method Name to a Method blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-24 19:15 +0000
  Re: Passing a Method Name to a Method Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-22 22:09 -0400
    Re: Passing a Method Name to a Method Gene Wirchenko <genew@ocis.net> - 2011-06-23 09:07 -0700
      Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-23 13:43 -0400
        Re: Passing a Method Name to a Method Nebulous <nebulous99@gmail.com> - 2011-06-23 13:57 -0700
      Re: Passing a Method Name to a Method Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-24 07:28 -0400
    Re: Passing a Method Name to a Method blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-23 17:53 +0000
  Re: Passing a Method Name to a Method Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-22 22:30 -0400
    Re: Passing a Method Name to a Method Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-23 09:06 -0300
      Re: Passing a Method Name to a Method Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-23 13:23 -0300
        Re: Passing a Method Name to a Method Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-23 09:33 -0700
          Re: Passing a Method Name to a Method Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-23 17:53 -0300
            Re: Passing a Method Name to a Method Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-23 19:15 -0700
              Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-24 01:48 -0400
                Re: Passing a Method Name to a Method Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-24 02:20 -0400
                Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-25 15:26 -0400
      Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-23 13:42 -0400
        Re: Passing a Method Name to a Method markspace <-@.> - 2011-06-23 11:32 -0700
          Re: Passing a Method Name to a Method Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-06-23 16:23 -0300
          Re: Passing a Method Name to a Method "Fuschia, President-Elect of the Bright Purplish-Green Council" <fp-eotbp-gc@ibm.com> - 2011-06-23 15:46 -0400
      Re: Passing a Method Name to a Method Gene Wirchenko <genew@ocis.net> - 2011-06-23 16:29 -0700
    Re: Passing a Method Name to a Method Gene Wirchenko <genew@ocis.net> - 2011-06-23 14:44 -0700
      Re: Passing a Method Name to a Method Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-24 07:48 -0400
  Re: Passing a Method Name to a Method Roedy Green <see_website@mindprod.com.invalid> - 2011-06-23 09:43 -0700

csiph-web