Android Learning key event monitoring and command mode
?
one Command mode
Intention:
Encapsulates a request as an object so that you can parameterize the customer with different requests;
Queue or log request logs for requests, and support for actions that can be undone.
The request is encapsulated as an object, and when a request is submitted to an object, it allows us to not have to know the specific requested operation or the recipient of the request.
It implements the decoupling between the requestor object of the action and the performer object of the action.
Applicability:
- Use the command mode instead of the callback application in callback form;
- Specify, arrange, and execute requests at different times, dynamic change requests;
- Support Cancel operation;
- Support to modify the log, save the request execution process to restore operations;
- The command mode supports invocation operations for a set of transactions. and easy to increase the expansion of new transactions;?
(What is a transaction reference: http://book.51cto.com/art/201202/319377.htm)
Structure:
??????
Description
Command: The wrapper of the order, which declares the operation interface of the command execution;
Concretecommand: The specific receiver of the command to perform the corresponding action;
Invoke: Issuing a request to execute the command, storing the abstract parent class of the specific command performer;
Receiver: The implementation and execution of specific related requests, specified by the creator of the Command object;
?
This diagram allows you to get to the core of this pattern: the implementation of invoke invoke of the action object is decoupled from the receiver that specifically implements the operation,
is to execute a call to the real action performer by adding the command to the command to perform the execution of the action.
?
two android in Onclicklistener
In the View class:
Public interface Onclicklistener {
void OnClick (View v);
}
function: Interface definition for a callback to being invoked when a view is clicked.
is a callback interface for the Click event.
In the view that needs to respond to the Click event, you need to register a corresponding clicklistener to listen for the Click event that is called when it occurs.
Take a look at the following code: Button control
Public classMyActivityextendsActivity {protected voidonCreate (Bundle icicle) {Super. OnCreate (Icicle); Setcontentview (r.layout.content_layout_id); FinalButton Button =(Button) Findviewbyid (r.id.button_id); //Register the Click event ListenerButton.setonclicklistener (NewView.btnonclicklistener ()); }Private classBtnonclicklistenerextendsonclicklistener{ Public voidOnClick (View v) {//Perform Action on Click }}}
?
Take a look at the corresponding structure: ?
?
The view is Invoke,onclicklistener or command,myactivity is receiver.
The execution process is as follows:
?
You can see that the application of this method belongs to:
The command mode replaces the callback model in the form of the callback function, making the frame structure clearer and more flexible.