Command mode 2 Invoker Vs. Client

Source: Internet
Author: User

Command mode 2 Invoker Vs. Client

When the program directly writes a command-issuing statement such as new route 1(cmd.exe cute (),The caller and customer are combined into one;

In GUI programs, statements that issue commands are usually included in the underlying framework, orThe underlying framework contains the callerAt this time, the program compiled by the programmer is a customer class.

In GUI programs, when a certain interface event occurs, the system acts as a command callerAutomatic CallCommand.


The following demo uses buttons, drop-down menus (JMenuBar-JMenu-JMenuItem), and pop-up menus (JPopupMenu ), JButton, JMenuItem, and JPopupMenu can inject an Action command through its constructor. (Dependency injection)

Next we will write two subclass of AbstractAction, which will be injected into the corresponding control.

It is worth noting that, The specific command class implements all functions by itself without additional recipients.In this case, the command mode (two sub-commands are compiled) can be applied, and the callback (two classes containing the callback function are compiled) can be simply applied ).

In the opinion of yqj2065, the concept of command mode is better:The specific command specifies the executor (which can be an abstract type) and the called method to decouple the C/S structure from S.

GoF:"Encapsulate (client) requests into an object so that you can parameterize the customer with different requests.". Too general. "Encapsulating (client) requests into an object" is a general picture of the Java interface. It refers to the parameter .exe () provided by the customer. In the case of polymorphism, it is "parameterized the customer with different requests". When there is no need for a receiver, we can even regard the action=med (ActionEvent evt) of AbstractAction as a policy, which is also acceptable. Do we say that this routine uses the policy mode ??

In any case, this application in the GUI is a typical case of command mode.

If you use multiple buttons in your program, such as Exit, you can write a command in command mode, instead of using anonymous classes anywhere..

Routine 3-22 command package method. command. gui; import javax. swing. *; import java. awt. event. *; import java. awt. *; class SubmitAction extends actaction {private Component target; public SubmitAction (String name, Icon, Component t) {putValue (Action. NAME, name); putValue (Action. SMALL_ICON, icon); putValue (Action. SHORT_DESCRIPTION, name + "the program"); target = t ;}@ Override public void actionreceivmed (ActionEvent evt) {JOptionPane. showMessageDialog (target, "submit action") ;}} class ExitAction extends actaction {private Component target; public ExitAction (String name, Icon, Component t) {putValue (Action. NAME, name); putValue (Action. SMALL_ICON, icon); putValue (Action. SHORT_DESCRIPTION, name + "the program"); target = t ;}@ Override public void actionreceivmed (ActionEvent evt) {int Answer = JOptionPane. showConfirmDialog (target, "Are you sure you want to exit? "," Confirmation ", JOptionPane. YES_NO_OPTION); if (answer = JOptionPane. YES_OPTION) System. exit (0) ;}} public class Test extends JFrame {Test () {Action ea = new ExitAction ("Exit", null, this ); action sa = new SubmitAction ("Submit", null, this); // drop-down menu (JMenuBar-JMenu-JMenuItem) JMenuBar bar = new JMenuBar (); JMenu mn_demo = new JMenu ("Demo"); mn_demo.add (new JMenuItem (sa); mn_demo.add (new JMenuItem (ea); bar. add (mn_demo); this. setJMenuBar (bar); // JPopupMenu final JPopupMenu pop = new JPopupMenu ("PopMenu"); pop. add (sa); pop. add (ea); // addMouseListener (new MouseAdapter () {public void mousePressed (MouseEvent e) {showPopup (e);} public void mouseReleased (MouseEvent e) {showPopup (e);} private void showPopup (MouseEvent e) {if (e. isPopupTrigger () {pop. show (e. getComponent (), e. getX (), e. getY () ;}}); JPanel jp = new JPanel (); JButton subbtn = new JButton (sa); JButton exitbtn = new JButton (ea); jp. add (subbtn); jp. add (exitbtn); Container con = getContentPane (); con. add (jp, "South"); setTitle ("Command pattern example"); setSize (300,200); setVisible (true );}}


Related Article

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.