swing - Using Factory Pattern in Java -


i studying factory pattern , have doubts best way buil it. developed 2 progamas same, difference code implements pattern in question. after comparison me find:

  1. code 1 better reusability, jvm, performance, etc, number 1 or 2?
  2. is 3.er optimal way more?

shared classes

artist

    public class artistframe extends abstractfactoryjinternalframe {  public artistframe(string title, int x, int y) {     super(title, x, y); }  @override void makebuttons() {      jbutton addbtn = new jbutton("add");     addbtn.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent actionevent) {             //to         }     });      jbutton savebtn = new jbutton("save");     savebtn.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent actionevent) {             //to         }     });      jbutton deletebtn = new jbutton("delete");     deletebtn.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent actionevent) {             //to         }     });      jbutton cancelbtn = new jbutton("cancel");     cancelbtn.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent actionevent) {             //to         }     });      add(addbtn, borderlayout.south);     add(savebtn, borderlayout.north);     add(deletebtn, borderlayout.east);     add(cancelbtn, borderlayout.west);  } } 

track

public class trackframe extends abstractfactoryjinternalframe {      public trackframe(string title, int x, int y) {         super(title, x, y);      }      @override     void makebuttons() {          jbutton addbtn = new jbutton("add");         addbtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton savebtn = new jbutton("save");         savebtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton deletebtn = new jbutton("delete");         deletebtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton cancelbtn = new jbutton("cancel");         cancelbtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          add(addbtn, borderlayout.north);         add(savebtn, borderlayout.center);         add(deletebtn, borderlayout.east);         add(cancelbtn,borderlayout.west);      } } 

album

public class albumframe extends abstractfactoryjinternalframe {      public albumframe(string title, int x, int y) {         super(title, x, y);      }      @override     void makebuttons() {          jbutton addbtn = new jbutton("add");         addbtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton savebtn = new jbutton("save");         savebtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton deletebtn = new jbutton("delete");         deletebtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          jbutton cancelbtn = new jbutton("cancel");         cancelbtn.addactionlistener(new actionlistener() {             @override             public void actionperformed(actionevent actionevent) {                 //to             }         });          add(addbtn, borderlayout.east);         add(savebtn, borderlayout.west);         add(deletebtn, borderlayout.north);         add(cancelbtn, borderlayout.south);      } } 

case 1
main class

public class testswing extends jframe {      jdesktoppane desktop;     abstractfactoryjinternalframe artistframe;     abstractfactoryjinternalframe albumframe;     abstractfactoryjinternalframe trackframe;      public testswing() {          setdefaultcloseoperation(windowconstants.exit_on_close);         setsize(500,300);          desktop = new jdesktoppane();          artistframe = abstractfactoryjinternalframe.getframe("artist",10,10);         albumframe = abstractfactoryjinternalframe.getframe("album", 20, 20);         trackframe = abstractfactoryjinternalframe.getframe("track", 30,30);          desktop.add(artistframe);         desktop.add(albumframe);         desktop.add(trackframe);          setcontentpane(desktop);          setvisible(true);      }      public static void main(string[] args) {          swingutilities.invokelater(new runnable() {             @override             public void run() {                 testswing ts = new testswing();             }         });      } } 

factory class public abstract class abstractfactoryjinternalframe extends jinternalframe {

protected abstractfactoryjinternalframe(string title, int x, int y) {     super(title, true, true, true, true);     setlocation(y,y);  }  public static abstractfactoryjinternalframe getframe(string title, int x, int y) {      abstractfactoryjinternalframe frame = null;      if (title.equals("artist")) {         frame = new artistframe(title, x, y);      } else if (title.equals("album")) {         frame = new albumframe(title, x, y);      } else {         frame = new trackframe(title, x, y);     }      frame.makebuttons();     frame.pack();     frame.setvisible(true);      return frame;  }  abstract void makebuttons(); } 

case 2
main class

public class testswing extends jframe {      jdesktoppane desktop;      abstractfactoryjinternalframe artistframe;     abstractfactoryjinternalframe albumframe;     abstractfactoryjinternalframe trackframe;      public testswing() {          setdefaultcloseoperation(windowconstants.exit_on_close);         setsize(500,300);          desktop = new jdesktoppane();          artistframe = new artistframe("arist",10,10);         albumframe = new albumframe("album", 20, 20);         trackframe = new trackframe("track", 30,30);          desktop.add(artistframe);         desktop.add(albumframe);         desktop.add(trackframe);          setcontentpane(desktop);          setvisible(true);     }       public static void main(string[] args) {          swingutilities.invokelater(new runnable() {             @override             public void run() {                 testswing ts = new testswing();             }         });      } } 

factory class

    public abstract class abstractfactoryjinternalframe extends jinternalframe {  protected abstractfactoryjinternalframe(string title, int x, int y) {     super(title, true, true, true, true);     setlocation(y,y);     makebuttons();     pack();     setvisible(true);   }  abstract void makebuttons(); } 

in fact, case#2 doesn't seem "factory" since using operator new create instances. in other hand, case#1 seems correct, don't know getframe(..) because code isn't posted.

 artistframe = abstractfactoryjinternalframe.getframe("artist",10,10); 

anyway, should check link factory pattern


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -