Using an interface and generics to draw a button in Java -


a java project contains method draw "edit" button. can please explain benefit of using interface here? interface can more simple jbotton button = new jbutton(); cannot?

private valueobject<?> getbuttonvalueobject() {         valueobject<?> buttonvalue = new mutablevalueobject<object>() {             private string title = "edit";               @override             public object getvalue() {                 return new abstractaction(title) {                        public void actionperformed(actionevent e) {                         final jdialog dialog = new jdialog();                         dialog.settitle("values");                         dialog.setlayout(new borderlayout());                                                 jpanel panel = new jpanel();                         dialog.add(panel, borderlayout.north);                         .......                          dialog.setvisible(true);                     }                      @override                     public string tostring() {                         return title;                     }                 };             }         };         return buttonvalue;     }   //valueobject.java public interface valueobject<t> {     t getvalue(); } 

there lot of problems in code not mention overcomplicating problem. there principal called kiss , kind of code goes straight against it.

some problems details:

  • private method abstraction without using custom (in posted code).
  • anonymus (inner) class usage not preferred. some readings here.

additional bad practices:

  • should private string title = "edit"; changeable? no -> why not static final?
  • dialog.settitle("values"); contains hard-wired string.
  • names not indicate intended "valueobject", "mutablevalueobject"

tl.dr: not example of generics. try keep source code simple humans read.


Comments