java - Is it appropriate to use a series of lambda functions to define class functionality? -


in recent discussion, friend , have disagreed on following use of lambda functions define class functionality. when creating object dynamic values, should dynamic values passed using lambdas, or provided using overriden methods in custom subclass?

consider following example, in goal have custom label component dynamic text , icon traits. label must have methods gettext() , geticon(). following 2 examples: 1 using multiple lambdas, , 1 defining subclass.

lambda approach:

class label {     private supplier<string> text;     private supplier<image> icon;      public label(supplier<string> text, supplier<image> icon) {         this.text = text;         this.icon = icon;     }      public string gettext() {         return text.get();     }      public image geticon() {         return icon.get();     } } 

use:

label timelabel = new label(     () -> system.currenttimemillis(),     () -> clock_image ); 

subclass approach:

class label {     private string text;     private image icon;      public label() {         // default     }      public label(string text, image icon) {         // set fields     }      // simple getters } 

use:

class timelabel extends label {     public string gettext() {         return system.currenttimemillis();     }      public image getimage() {         return clock_image;     } }  label timelabel = new timelabel(); 

which of these approaches more appropriate, considering expectations, readability, , usability both internal , external developers (including class api)?

lambdas have uses, on use. note: when create lambda have build in assumption parameters using. couldn't externally define lambda uses protected field example.

lambdas come overhead, both in terms of cpu/memory , conceptual overhead. new class generated @ runtime , if profiling or debugging tools can untangle code much.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -