"Command mode"

Source: Internet
Author: User

The command mode is one of the gof23 modes and the behavior mode. This mode is hard to understand. The design pattern is not detailed in its book. Some articles on the Internet have incorrect interpretations.

In fact, the command mode is not so mysterious. In this article, I will tell you what the command mode is and how to use the command mode.

The key to understanding the command mode is:

1. Use interfaces to implement classes.

Generally, there is only one method in the command mode interface.

Methods of implementation classes have different functions, covering methods in interfaces.

In object-oriented programming, if… is widely used... Else ..., Or switch... Case... Such a condition selection statement is the "worst practice ". This type of code usually means there is room for refactoring.

Command mode is a powerful tool for killing conditional selection statements.

1. First, provide an interface:

Public interface command {

Public void execute ();

}

 

2. then provide the Implementation class of this interface. The method of each implementation class is if... Else... Code in a code block.

In this way, the caller can directly upload an instance of a specific class. For example:

Public void test (command para ){

Para.exe cute ();

 

}

You do not need to determine which piece of code should be executed in which case.

All problems are handled by the caller.

If the command mode is not used, if the number of cases increases gradually, for example, from the original two to 20, the number of judgments in the method increases from 1 to 19.

In the command mode, the caller only needs to add two implementation classes to 20 Implementation classes. The above test method does not need to be changed at all.

 

II. The main purpose is to use the parameter callback mode.

The most important way to use the command mode is to use the parameter callback mode.

The command interface is passed in as a method parameter. Then, callback the interface in the method body.

As shown in the preceding example, swing's event mechanism also uses this method.

For example:

Public interface command {

Public void execute ();

}

 

Public void actionreceivmed (actionevent e ){

Command cmd = (command) E. getsource ();

Cmd.exe cute ();

}

 

Of course, the command mode can also be used in other ways. It is not necessarily in the parameter callback mode.

The core idea of the command mode is to pass an instance of a specific class with a method to the user as an interface. The specific type information of the object disappears.

Call the method of this interface after obtaining this interface in the user's code.

The specific execution result depends on the Implementation class of the object provided by the command initiator. This gives the command initiator full control, while the user's Code does not care about the specific command classes and methods. It also makes the condition judgment statement redundant.

Simple? The command mode is actually so simple.

 

PS:

In fact, several of the 23 gof design patterns use the same technique. Gof divides patterns based on purpose rather than skill. Therefore, the command mode and Policy mode actually use the same techniques.

Last time I talked to a colleague about technology. His background is Windows C ++ and windows driver development. He said he does not know any design patterns. He thinks the most useful thing is the callback function.

 

The command mode also uses callback functions. Java does not have function pointers. Everything in Java is an instance of classes and classes. Therefore, you need to use an interface with only one function, and its instance represents the function pointer. It is actually one thing.

 

Design Patterns are relatively low-level design ideas. At a higher level, there are still more macro design skills. Uncle Bob has a good book and has forgotten his name. "Unix programming art" is also good, all about higher-level design.

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.