Method References

Rémi Forax forax at univ-mlv.fr
Fri Feb 26 00:42:04 PST 2010


Le 26/02/2010 08:40, Neal Gafter a écrit :
> On Thu, Feb 25, 2010 at 11:27 PM, Paulo Levi<i30817 at gmail.com>  wrote:
>
>    
>> What about not "this" objects.
>>
>>      
> See CfJ<http://www.javac.info/closures-v06a.html>  for my specific proposal,
> under the section heading "Method References".  The part before # is an
> arbitrary expression.
>
> In fact would work on constructors or does it need the reference.
>    
>>      
> I don't think constructors are a frequent enough use case that explicit
> support for them is needed as a language feature, but that could be done
> too.
>    

Having constructors enable to write the Design Pattern abstract factory 
in few lines.
(reusing the example from wikipedia about abstract factory)

interface  GUIFactory{
     public  Button  createButton();
}

class  Application{
     public  Application(GUIFactory factory){
         Button  button=  factory.createButton();
         button.paint();
     }
}

public  class  ApplicationRunner{
     public  static  void  main(String[]  args)  {
         new  Application(createOsSpecificFactory());
     }

     public  static  GUIFactory createOsSpecificFactory()  {
         int  sys=  readFromConfigFile("OS_TYPE");
         if  (sys==  0)  {
             return  WinButton#new();
         }  else  {
             return  OSXButton#new();
         }
     }
}

Rémi



More information about the lambda-dev mailing list