codepromo0.fr
MasterofProject

"Android source design pattern analysis and actual combat" reading notes (eleven)

Label Androidreading notesdesign pattern
13 people read comments(0) Collection Report
Classification:

The eleventh chapter, command mode

Command mode is one of the behavior patterns. Overall, it is not difficult to understand, but more cumbersome, he will simply call the relationship between the decoupling into a number of parts, increase the complexity of the class, but even so, the structure of the command pattern is still clear.

1 Definition

Encapsulate a request into an object, which allows the user to use a different request to the client to parameterized; request a queue or record the request log, and support for the revocation of the operation.

2 usage scenarios

(1) to take out the action to be executed, and then to provide the parameters in the form of.

(2) specify, arrange, and execute requests at different times. A command object can have a lifetime that has nothing to do with the initial request.

(3) the need to support the operation of the cancellation.

(4) support to modify the log function, so that when the system crashes, these changes can be done again.

(5) need to support business operations.

3 simple implementation

The Sokoban game as an example, the general game will have five buttons are left, right, up and down, revocation. Then play games is the client, five button is the caller, the implementation of specific command button command role.

Receiver role: push box game

Public Class PushBox{
    / * *
* execute the command to the left
* /
    Public Void ToLeft(1)
System.out.println ("To the left");
}

    / * *
* execute to the right command
* /
    Public Void ToRight(1)
System.out.println ("To the right");
}

    / * *
* execute down command
* /
    Public Void ToDown(1)
System.out.println ("Down");
}

    / * *
* execute up command
* /
    Public Void ToUp(1)
System.out.println ("Up");
}

    / * *
* execute the undo command
* /
    Public Void Revoke(1)
System.out.println ("Revocation");
}
}

Command abstraction

Public Interface Command{
    / * *
* command execution method
* /
    VoidExecute ();

    / * *
* get command type
* /
    VoidGetCommand ();
}

The specific command, the left command class

Public Class LeftCommand Implements Command{

    / / holds a reference to accept the Sokoban game object
    PrivatePushBox PushBox;

    Public LeftCommand(pushBox PushBox) {
        This.pushBox = pushBox;
}

    @Override
    Public Void Execute(1)
        / / call specific orders
PushBox.toLeft ();
}

    @Override
    Public Void GetCommand(1)
System.out.print ("To the left -- >");
}
}

The specific command, the command class.

Public Class RightCommand Implements Command{

    / / holds a reference to accept the Sokoban game object
    PrivatePushBox PushBox;

    Public RightCommand(pushBox PushBox) {
        This.pushBox = pushBox;
}

    @Override
    Public Void Execute(1)
        / / call specific orders
PushBox.toRight ();
}

    @Override
    Public Void GetCommand(1)
System.out.print ("Right -- >");
}
}

Specific commands, up the command class

Public Class UpCommand Implements Command{

    / / holds a reference to accept the Sokoban game object
    PrivatePushBox PushBox;

    Public UpCommand(pushBox PushBox) {
        This.pushBox = pushBox;
}

    @Override
    Public Void Execute(1)
        / / call specific orders
PushBox.toUp ();
}

    @Override
    Public Void GetCommand(1)
System.out.print ("Up /");
}
}

Specific commands, down command class

Public Class DownCommand Implements Command{

    / / holds a reference to accept the Sokoban game object
    PrivatePushBox PushBox;

    Public DownCommand(pushBox PushBox) {
        This.pushBox = pushBox;
}

    @Override
    Public Void Execute(1)
        / / call specific orders
PushBox.toDown ();
}

    @Override
    Public Void GetCommand(1)
System.out.print ("Down -- >");
}
}

Specific order, revocation of the command class

Public Class RevokeCommand Implements Command{

    / /持有一个接受推箱子游戏对象的引用
    私人推箱子推箱子;

    公共 revokecommand(推箱子推箱子){
        推箱子,推箱子;
}

    “重写”
    公共 无效 执行(){
        / /调用具体命令
revoke()推箱子;;
}
    “重写”
    公共 无效 getcommand(){
}
}

请求者类,命令由按钮发起

公共  按钮{

    私人leftcommand leftcommand;/ /向左移动的命令对象引用
    私人rightcommand rightcommand;/ /向右移动的命令对象引用
    私人upcommand upcommand;/ /向上移动的命令对象引用
    私人downcommand downcommand;/ /向下移动的命令对象引用
    私人revokecommand revokecommand;/ /撤销命令对象引用

    私人ArrayList <命令> commandlist =新的ArrayList <命令>();/ /用于记录命令动作

    * * * *
*获取执行命令
*
    公共 无效 getcommandlist(){
        对于(指挥:commandlist){
C. getcommand();
}
系统,println(。“”);
}

    * * * *
*设置向左移动的命令对象
*
*@参数leftcommand向左移动的命令对象
*
    公共 无效 setleftcommand(leftcommand leftcommand){
        leftcommand = leftcommand;
}

    * * * *
*设置向右移动的命令对象
*
*@参数rightcommand向右移动的命令对象
*
    公共 无效 setrightcommand(rightcommand rightcommand){
        rightcommand = rightcommand;
}

    * * * *
*设置向上移动的命令对象
*
*@参数upcommand向上移动的命令对象
*
    公共 无效 setupcommand(upcommand upcommand){
        upcommand = upcommand;
}

    * * * *
*设置向下移动的命令对象
*
*@参数downcommand向下移动的命令对象
*
    公共 无效 setdowncommand(downcommand downcommand){
        downcommand = downcommand;
}

    * * * *
*设置撤销命令对象
*
*@参数revokecommand撤销命令对象
*
    公共 无效 setrevokecommand(revokecommand revokecommand){
        revokecommand = revokecommand;
}

    * * * *
*按下向左按钮
*
    公共 无效 试验(){
leftcommand。execute();
commandlist添加(leftcommand);
}

    * * * *
*按下向右按钮
*
    公共 无效 向右(){
rightcommand。execute();
commandlist添加(rightcommand);
}

    * * * *
*按下向上按钮
*
    公共 无效 (){
upcommand。execute();
commandlist添加(upcommand);
}

    * * * *
*按下向下按钮
*
    公共 无效 顾客(){
downcommand。execute();
commandlist添加(downcommand);
}

    * * * *
*按下撤销按钮
*
    公共 无效 撤销权(){
revokecommand。execute();
commandlist。删除(commandlist。size()—);
}
}

客户端调用

公共 客户{

    公共 静态 无效 主要(string [] args){
        / /首先创建游戏
推箱子推箱子=新的pushbox();

        5 /根据游戏构造种命令
leftcommand leftcommand =新的leftcommand(推箱子);
rightcommand rightcommand =新的rightcommand(推箱子);
upcommand upcommand =新的upcommand(推箱子);
downcommand downcommand =新的downcommand(推箱子);
revokecommand revokecommand =新的revokecommand(推箱子);

        / /按钮可以执行不同命令
按钮按钮新的buttons();
setleftcommand(leftcommand)按钮;
setrightcommand(rightcommand)按钮;
setupcommand(upcommand)按钮;
setdowncommand(downcommand)按钮;
setrevokecommand(revokecommand)按钮;

        / /执行操作
toleft()按钮;
todown()按钮;
todown()按钮;
toright()按钮;
getcommandlist()按钮;
torevoke()按钮;
toup()按钮;
toleft()按钮;
todown()按钮;
toup()按钮;
getcommandlist()按钮;
}
}

执行结果

向左
向下
向下
向右
向左>向下>向下>向右>
撤销
向上
向左
向下
向上
向左>向下>向下>向上>向左>向下>向上>

在这么长的代码之后是不是觉得很烦琐,明明可以很简单的实现,如下:

公共 客户{

    公共 静态 无效 主要(string [] args){
        / /首先创建游戏
推箱子推箱子=新的pushbox();

todown()推箱子;
PushBox.toRight ();
PushBox.toUp ();

}
}

In fact, the design pattern has an important principle: to modify the closure of the expansion of open. If you use the simple method as above, then the following modifications can only be used to modify the PushBox class, and then modify the Client class, which is obviously contrary to the principle of. If you use the command mode, then the Client class does not need to be modified, only need to modify the internal operation of the PushBox class, the Client class does not need to know the specific internal implementation.

The use of design patterns have mentioned before, mainly to see after the scene complexity and the demand expansion, maintenance and other aspects, full use of design patterns is not recommended, which requires designers to weigh up all the pros and cons.

4.Android source code in the implementation of the command mode

1.PackageHandler

PackageManagerService, the package of the relevant message processing right of its internal class PackageHandler bear, which will need to deal with the request as an object to pass the message to the relevant method, and for the package installation, mobile and package size measurement are encapsulated asHandlerParamsSpecific subclass ofInstallParams,MoveParamsandMeasureParams. Source code is as follows:

Private Abstract Class HandlerParams{
        Private Static Final IntMAX_RETRIES =Four;

        / * *
* of times startCopy has () been attempted and had a non-fatal Number
* error.
* /
        Private IntMRetries =Zero;

        Final BooleanStartCopy () {
            BooleanRes;
            Try{
                If(DEBUG_INSTALL) Slog.i (TAG),"StartCopy");

                If(++mRetries > MAX_RETRIES) {
Slog.w (TAG,"To invoke remote methods on default Failed container service. Giving up");
MHandler.sendEmptyMessage (MCS_GIVE_UP);
HandleServiceError ();
                    Return False;
}Else{
HandleStartCopy ();
= resTrue;
}
}Catch(E RemoteException) {
                If(DEBUG_INSTALL) Slog.i (TAG),"Install MCS_RECONNECT Posting");
MHandler.sendEmptyMessage (MCS_RECONNECT);
= resFalse;
}
HandleReturnCode ();
            ReturnRes;
}

        Final VoidServiceError () {
            If(DEBUG_INSTALL) Slog.i (TAG),"ServiceError");
HandleServiceError ();
HandleReturnCode ();
}

        Abstract VoidHandleStartCopy ()ThrowsRemoteException;
        Abstract VoidHandleServiceError ();
        Abstract VoidHandleReturnCode ();
}

Can seeHandlerParamsAn abstract command.

5 Summary

1 advantages

The encapsulation of the command mode is very good, more weak coupling, more flexible control and better scalability.

2 shortcomings

Class expansion, the creation of a large number of derivatives.

6 reference

Link:command mode

top
Zero
tread
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit4815 times
    • Integral:Three hundred and ninety-one
    • Grade
    • Rank:Thousands of miles away
    • Original32
    • Reproduced:4
    • Translation:0
    • Comments:2
    Classification of articles
    Latest comments
gutscheincode0.de