Use command mode to say goodbye to if-Else

Source: Internet
Author: User

Let's look at the example below: The following button is used as a command

Package command; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jpanel; public class buttonpanel extends jpanel implements actionlistener {Private Static final long serialversionuid = 4715122374780095560l; private jbutton Bt1, bt2, bt3; Public buttonpanel () {Bt1 = new jbutton1 ("button 1"); bt1.addactionlistener (this); bt2 = new jbutton2 ("button 2"); bt2.addactionlistener (this ); bt3 = new jbutton3 ("button 3"); bt3.addactionlistener (this); add (Bt1); add (bt2); add (bt3);} public void actionreceivmed (actionevent E) {jbutton bt = (jbutton) E. getsource (); If (bt = Bt1) {system. out. println ("You clicked" + bt. gettext ();} else if (bt = bt2) {system. out. println ("You clicked" + bt. gettext ();} else {system. out. println ("You clicked" + bt. gettext () ;}// a button ---> one command private class jbutton1 extends jbutton {public jbutton1 (string text) {super (text );}} private class jbutton2 extends jbutton {public jbutton2 (string text) {super (text) ;}} private class jbutton3 extends jbutton {public jbutton3 (string text) {super (text );}} public static void main (string [] ARGs) {jframe frame = new jframe (); frame. add (New buttonpanel (); frame. setsize (300,100); frame. setvisible (true); frame. setdefaclocloseoperation (jframe. exit_on_close );}}

If you use the command mode to write:
Package command; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jpanel; public class buttonpanel1 extends jpanel implements actionlistener {Private Static final long serialversionuid = 4715122374780095560l; private jbutton Bt1, bt2, bt3; public interface command {public void excute ();} public buttonpanel1 () {Bt1 = new jbutton1 (" 1"); bt1.addactionlistener (this); bt2 = new jbutton2 (" 2"); bt2.addactionlistener (this ); bt3 = new jbutton3 ("button 3"); bt3.addactionlistener (this); add (Bt1); add (bt2); add (bt3);} public void actionreceivmed (actionevent E) {command = (command) E. getsource (); command. excute ();} private class jbutton1 extends jbutton implements command {private string text; Public jbutton1 (string text) {super (text); this. TEXT = text;} public void excute () {system. out. println ("You clicked" + text) ;}} private class jbutton2 extends jbutton implements command {private string text; Public jbutton2 (string text) {super (text); this. TEXT = text;} public void excute () {system. out. println ("You clicked" + text) ;}} private class jbutton3 extends jbutton implements command {private string text; Public jbutton3 (string text) {super (text); this. TEXT = text;} public void excute () {system. out. println ("You clicked" + text) ;}} public static void main (string [] ARGs) {jframe frame = new jframe (); frame. add (New buttonpanel1 (); frame. setsize (300,100); frame. setvisible (true); frame. setdefaclocloseoperation (jframe. exit_on_close );}}
The second type is displayed. If-else is no longer requiredIf you add one more button (one command) to the two classes at this time, you must add one more if-Else To The actiontailmed class before the other class. Therefore, the command mode can Simplifies program control, and can easily add new command classes.. In addition, the command mode has other advantages, such Improves loose coupling and reduces the dependency between classes.And so on. (The internal classes in the above example are separated separately to reflect loose coupling .)
The following is an example of Java advanced programming:
Package layout; import Java. AWT. borderlayout; import Java. AWT. color; import Java. AWT. gridlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. util. hashtable; import javax. swing. buttongroup; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jlabel; import javax. swing. jpanel; import javax. swing. jprogressbar; import javax. swing. jradiobutton; // implements the command mode interface Then, the application can derive the appropriate event action public class borderlayoutpanel extends jpanel implements actionlistener {Private Static final long serialversionuid = 6332313305423197159l according to the user navigation operations at runtime; private Final Static string buttontext = "<HTML> <center> <font size = '+ 2'> basic arithmetic </font>" + "<br/> click obtain question </center> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.