This article describes the implementation of post office sending function based on command mode in Java. Share it for your reference, as follows:
1. Pattern definition
Command mode, feuding the request from the client as an object, without understanding the action of the request activation or processing details about accepting the request. The fundamental purpose of the command mode is to decouple between the "requester" and the "implementer".
2. Model examples
1 Pattern Analysis
We borrowed post office letters to illustrate this pattern.
2 Command mode static class diagram
3 Code Examples
3.1 Letter Receiver Interface-IReceiver
package com.demo.receiver;/** * Letter Receiver interface* * @author * */public interface IReceiver { /** * The recipient receives the letter to read* * @param message */ public void readMail(String message);}3.2 Letter Receiver
package com.demo.receiver;/** * Letter recipient* * @author * */public class Receiver implements IReceiver { /** * Recipient received the letter to read* * @param message */ public void readMail(String message) { System.out.println("Recipient reads the letter: " + message); }}3.3 Post Office Interface-IPost
package com.demo.command;/** * Post Office interface* * @author * */public interface IPost { /** * Post Office sends letters* * @param message */ public void sendMail(String message);}3.4 Post office implementation
package com.demo.command;import com.demo.receiver.IReceiver;/** * Post office implementation class* * @author * */public class Post implements IPost { // Recipient object instance private final IReceiver receiver; /** * Constructor method to send to the recipient object instance* * @param receiver */ public Post(IReceiver receiver) { this.receiver = receiver; } /** * Post office sends letters* * @param message */ @Override public void sendMail(String message) { System.out.println("The post office sends the letter to the recipient..."); // The post office sends the letter to the recipient this.receiver.readMail(message); }}3.5 Message Sender - Invoker
package com.demo.invoker;import com.demo.command.IPost;/** * Sender* * @author * */public class Invoker { // Post office private attribute private IPost post; /** * Set post office object instance* * @param post */ public void setPost(IPost post) { this.post = post; } /** * Sender sends a letter* * @param message */ public void postMail(String message) { System.out.println("Sender sends a letter to the post office..."); this.post.sendMail(message); }}3.6 Let the post office start working a Client
package com.demo;import com.demo.command.IPost;import com.demo.command.Post;import com.demo.invoker.Invoker;import com.demo.receiver.Receiver;/** * Client application* * @author * */public class Client { /** * @param args */ public static void main(String[] args) { // Create a recipient object instance Receiver receiver = new Receiver(); // Create a post office object instance to pass in the recipient object instance IPost post = new Post(receiver); // Create a sender object instance and pass it into the post office object instance. Invoker invoker = new Invoker(); // Set the post office invoker.setPost(post); // The sender starts sending messages! ! ! invoker.postMail("Hello! Long time no see, are you busy with work recently?"); }}4 Running results
The sender delivers the letter to the post office...
The post office sends the letter to the recipient...
The recipient reads the letter: Hello! Long time no see, are you busy with work recently?
III. The design principles of this pattern
1 "Open-Close" principle
2 The principle of least knowledge
4. Command mode static class diagram
For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.