An example of the specific application of Command mode in SAP Spartacus

In object-oriented programming, the command pattern is a behavioral design pattern where objects are used to encapsulate all the information needed to perform an action or trigger an event later. This information includes the method name, the object that owns the method, and the values ​​of the method parameters.

The four terms associated with the command pattern are command, receiver, caller, and client. The command object knows the receiver and invokes the receiver's methods. The parameter values ​​of the receiver method are stored in the command. The receiver object that executes these methods is also stored in the command object via an aggregate. Then, when the execute() method in command is called, the receiver does the work. The caller object knows how to execute the command and can optionally log the command execution. The caller knows nothing about the specific command, it only knows the command interface. The caller object, command object, and receiver object are held by the client object, and the client decides which receiver objects to assign to the command object and which commands to assign to the caller. The client decides which commands to execute at which points. In order to execute the command, it passes the command object to the caller object.

The following is the specific implementation of command mode in SAP Spartacus.

The user module is not loaded until email address is clicked.

HTTP request after click:

http://localhost:4299/feature-libs_user_profile_public_api_ts.js

After clicking the save button, its form's onSubmitmethod is called:

onSubmitCall the this.service.save method.

A dependency of UpdateEmailComponentService userEmail, of type UserEmailFacade, whose update method implementation is a commandpattern .

The facade finally delegates to the concrete implementation class:

Use dependency injection to instantiate the implementation class:

First use this.command.create to create a Command instance, the type is Command, and pass in a type parameter, including password and newUid fields.

The create method receives two parameters, commandFactory is a factory function that receives the input parameter payload of the specific action to be executed.

The factory function is specifically executed here:

Generate a new command instance and return.

Later the update method of the email service is called.

Call the execute method of the command instance just returned.

Enter the execute method that just returned the dynamically created command instance.

Finally, the wrapper code of the factory method passed in when we call create to create a command instance is called:

cmd is the input parameter of the factory function:

Here, the factory function itself is also called by rxjs, and the connector.updateEmail method on line 30 is not directly executed.

The specific invocation of this method commands$.nextis .

Finally got it currentidand passed it as a parameter to the update email connector:

Modify the url of the email address: https://localhost:9002/occ/v2/powertools-spa/users/current/login?lang=en&curr=USD

payload:

There is no response.

More Jerry's original articles, all in: "Wang Zixi":

Guess you like

Origin blog.csdn.net/i042416/article/details/122968147