3.4 command mode (5.2)

Source: Internet
Author: User
1. Introduction

Assume that the TV/TV has methods of open (), close (), and changechannel () for opening, closing, and switching TV channels, while the remote control/controller for TV operations, the message transfer/method call expression is usually used.

package method.command;/** * @author yqj2065 * @version 0.1 */public class Controller1{    public static void foo(){TV tv = new TV();tv.open();tv.changeChannel();tv.close();}}
The service request or message passing expression in the object-oriented interface is as follows:

TV.Open ();

Unlike the function calls in imperative languages such as C, the message passing expression contains the Message Receiver TV, which is added with open ().

The command mode is a kind of uniform service request method.

  • The controller does not want to rememberMethod Name. Open (), close (), changechannel? Will there be more methods to call or request more services in the future? Therefore, I do not want to be restricted by the method name.EXE () as a universal method name, EXE () is "method objectization" into command -- use command to encapsulate EXE ().
  • Controller does not want to knowMessage recipientWho is it. The commander ordered the team to lay down the hill. Will he consider the company commander to take people to attack. Whoever cares about you, it is the concern of the commander who laid down this hill.

It is a little difficult to learn the command mode.

package method.command;public interface Command{public void exe();}

The following is a command mode that can be easily understood? Or from scratch, let's look at how the controller can forget/ignore the called method name?

2. Command and execution

Let's give an example of the command mode.

Since there is a command, it is also recommended to adopt a strategy mode with a polymorphism and a difficulty coefficient of 0,TV's open () evolved into the sub-class opencommand of command.

Opencommand has a private member, TV. What is opencommand's exe? Obviously, you only need one statement TV. open (). Write the code by yourself.

Because we have a dependency injection tool. god, (note: this tool is used in many articles on my blog, but the class name frompropertyfile, IOC, and God have also changed, I am too reluctant to modify relevant blog posts one by one. The meaning of the Code is clear, and the reader can modify the code ).

package method.command; import tool.God;public class Controller{public static void test(){    Command c1 = (Command)God.create("open");    c1.exe();}}
The Controller only knows the command object. The command in the controller is the string "open". The God creates the method. Command. opencommand object based on the string "open.

Ignore all details,Controller only depends on command, For comparison,Controller1 depends on TV, and the existing operation/method name of TV.



3.4 command mode (5.2)

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.