Implementing design Patterns with a Java 8 lambda expression: Command mode

Source: Internet
Author: User

Links: http://www.importnew.com/16789.html

In this blog, I'll show you how to implement a command design pattern when using a functional programmatic approach to Java 8 lambda expressions. The goal of the command pattern is to encapsulate the request as an object, from a different type of request to the client, such as a queue or log request parameterization, and provide the appropriate action. The command pattern is a common programming approach that executes methods based on the order in which they are run-time. The participants in the pattern are as follows:

    • Command: Declares the interface used to perform the operation.

    • Entity command: Defines the binding of the receiver object and the action.

    • Client: Creates an instance of the entity command and sets its recipient.

    • Caller: Control command to execute the request.

    • Receiver: The actual completion of the work.

The relationships between these participants are described as follows:

Let's look at a specific example of a command pattern to see how it is converted into a lambda expression. Suppose we have a file system tool that all actions rely on, such as opening a file, writing to a file, and closing a file. This enables macro functionality, where a series of operations can be recorded and then executed as a separate operation. Here are our recipients.

Public interface Filesystemreceiver {void  openFile (); void WriteFile (); void CloseFile ();}

Each operation is a command, such as openFile and WriteFile. We can create a common command interface to fit these different operations. Let's name the interface action, because in our context it represents an operation. All command objects need to implement this interface.

void perform ();}

Now, let's implement the action interface for each operation. All these classes need to do is call a method of Filereceiver and encapsulate the call into the action interface. After the encapsulation operation, use the appropriate class naming conventions to name these classes. Therefore, the class of the OpenFile method object is called OpenFile.

Public class OpenFile implements Action {private final filereceiver filereceiver;public OpenFile (filereceiver Filereceiver) {this. filereceiver =void  perform () {Filereceiver.openfile ();}}

Now, we implement the Macro class. Each macro contains an action sequence that can execute the action in turn, which acts as the caller. This class can record actions and run them together. We can store the action sequence in the list and then repeatedly fetch each action to execute.

New arraylist<>voidvoid  run () {Actions.foreach (Action::p erform);}}

When populating a macro object, we can add an instance of each command that is also recorded in the macro object. Now simply run the macro object, and each command executes sequentially. Our client code is as follows.

New Macro (); Macro.record (new  OpenFile (filereceiver)); Macro.record (new  WriteFile (Filereceiver)); Macro.record (new  CloseFile (filereceiver)); Macro.run ();

If you read this in my mind, you'll wonder how lambda expressions fit these. In fact, all command classes, such as OpenFile, WriteFile, and CloseFile, are actually just lambda expressions that want to break their encapsulation. These command classes are only passed between classes. The whole pattern of using lambda expressions is greatly simplified because we can completely abolish these classes. Let's see if the macro Class (client) uses a lambda expression instead of the command class's effect.

Filereceiver.closefile (), new--- Macro.run (); 

If you can realize that each lambda expression is executing a separate method call, you can further refine it. Therefore, you can use the method reference directly.

New Macro (); Macro.record (filereceiver::openfile); Macro.record (filereceiver::writefile); Macro.record ( Filereceiver::closefile); Macro.run ();

The command pattern is easy to extend, and new action methods can be added to the receiver to create a new command implementation so that no client code needs to be changed. The Runnable Interface (java.lang.Runnable) in the JDK is a popular interface commonly used in command mode. In this blog, I try to illustrate the command pattern with a Java 8 lambda expression. You can see the use of lambda expressions, with a lot less boilerplate code, to make your code neater.

Implementing design Patterns with a Java 8 lambda expression: Command mode

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.