Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Command Mode in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to implement the command mode in Java? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Command mode

The command mode is easy to understand. for example, the commander orders the soldier to do something, and from the point of view of the whole thing, the role of the commander is to issue a password, which is passed to the soldier's ear and carried out by the soldier. The good news of this process is that the three are decoupled from each other, and none of them need to rely on others, but just do their own thing. The commander wants the results and will not pay attention to how the soldiers achieve it. Let's take a look at the diagram:

Invoker is the caller (commander), Receiver is the callee (soldier), MyCommand is the command, implements the Command interface, holds the receiving object, and looks at the implementation code:

Public interface Command {public void exe ();} public class MyCommand implements Command {private Receiver receiver; public MyCommand (Receiver receiver) {this.receiver = receiver;} @ Override public void exe () {receiver.action ();}} public class Receiver {public void action () {System.out.println ("command received!") }} public class Invoker {private Command command; public Invoker (Command command) {this.command = command;} public void action () {command.exe ();}} public class Test {public static void main (String [] args) {Receiver receiver = new Receiver (); Command cmd = new MyCommand (receiver) Invoker invoker = new Invoker (cmd); invoker.action ();}}

It is easy to understand that the purpose of command mode is to decouple the issuer and executor of commands and separate request from execution. Students familiar with Struts should know that Struts is actually a technology that separates request from presentation, which inevitably involves the idea of command mode!

Introduction

Intent: encapsulate a request into an object so that you can parameterize the customer with different requests.

The main solution: in software systems, there is usually a tightly coupled relationship between behavior requesters and behavior implementers, but in some situations, such as recording, undo or redoing, transactions, and so on, this tightly coupled design that can not resist changes is not appropriate.

When to use: in some situations, such as "recording, undoing / redoing, transactions" of behavior, this tight coupling that cannot resist change is inappropriate. In this case, how to decouple the "behavior requester" from the "behavior implementer"? By abstracting a group of behaviors into objects, the loose coupling between the two can be realized.

How to solve it: call the recipient to execute the command through the caller, in the order: the caller → the recipient → command.

Key code: define three roles: 1, received real command execution object 2, Command 3, invoker uses the entry of command object

Application example: there is only one action core controller ActionServlet in struts 1, which is equivalent to Invoker, and the class of the model layer will have different model classes according to different applications, which is equivalent to the concrete Command.

Advantages: 1. Reduce the degree of system coupling. 2. New commands can be easily added to the system.

Cons: using command mode may result in some systems having too many specific command classes.

Usage scenario: command mode can be used wherever you think it is a command, for example: 1. Every button in GUI is a command. 2. Simulate CMD.

Note: the system needs to support command undo (Undo) operation and restore (Redo) operation, you can also consider using command mode, see the extension of command mode.

This is the answer to the question about how to implement the command mode in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report