java - Reflections bad or good practice for commands -


well, saw there questions , answers this, didn't satisfy me.

let's example, have programmed console. it's nice jframe output , input txtfield/area. console should not used output, run commands.

because need console , don't want change code of console programmed way:

the console has 1 method register commands.

console.registercommand(string command, string methodtoinvoke, object invokeobject); 

with method i'm able use console everywhere without need of changing or inharitance.

whenever string command written console knows it's registered keyword , executes method via reflection.

would or bad practice? on code styling , in performance! , better?

i found quite neat use reflections way add actionlisteners menuitems in trayicon.

edit

to answer below:

ok commands accept way do. in tray example wrote trayhelper class creates trayicon. there want add menuitems , actionlisteners without creating every object myself , add them tray. wrote methods this:

public void addmenuitem(string label, string methodtoinvoke, string invokeobject); 

this method not executes method when menuitem clicked, creates menuitem first, adds actionlistener invokes method, , adds trayicon.

so in order use trayhelper can write:

th.addmenuitem("exit","exitmethod",this);//executes exitmethod of                                          //this class after menuitem exit                                          //was clicked 

i don't see how without reflection other write objects myself again , adding them tray. or i'm blind :)

edit 2

ok, blind. didn't realize how without reflection, simple.

especially command pattern.

because of anonymous classes way, , way write code way (i did actionlisteners)

th.addmenuitem("test",new command(){        public void execute(){             //do stuff        } }); 

thank :)

there better way this. helps hide action done inside command object. have change command, don't have mess other code.

further, can have lot of different commands , can related inheritance or aggregation or injected each other needed , nobody else has know.

first have interface:

public interface command {     void execute(); } 

then have code take 1 of these:

console.registercommand(command command); 

then write various classes implement interface , something:

public class onecommand implements command {     public void execute() {         theobject.themethod(thecommand); // calls have reflection     } } 

this standard gof command pattern , can read more here: link wikipedia

note pattern, along other gof patterns, published in book in 1994. authors collected these best practices on many software projects. book in 40th printing (according wikipedia).

all suggests lots of people have found lots of reasons use these on many pieces of softwear, on many years , in many programming languages , systems.

it doesn't mean need use them use of tried , tested pattern avoid unseen pitfalls.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -