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 understand the Command pattern of Java Design pattern

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

Share

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

This article focuses on "how to understand the command pattern of Java design pattern", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the command pattern of Java design pattern.

What is the command mode

Command mode is a highly cohesive mode, which is defined as encapsulating a request into an object, so that you can parameterize the client with different requests, queue requests or log requests. Can provide command revocation and recovery functions.

In this class diagram, we see three roles:

Receiver recipient role: this role is the working role, and the command passed here should be executed.

Command command role: all commands that need to be executed are declared here

Invoker caller role: receives the command and executes the command

Second, the use scene of command mode

Timing: when you need to register a function and then call it later, you need to use the command mode, which is actually a callback function.

Sometimes you need to send a request to some object, but you don't know who the recipient of the request is or what the requested operation is. At this point, we want to design the program in a loosely coupled way, so that the request sender and the request receiver can eliminate the coupling relationship between each other.

Example: to order a meal, a guest needs to send a request to the chef, but he has no idea of the chef's name and contact information, nor does he know the way and steps of cooking. The command mode encapsulates the guest's request for ordering into a command object, that is, the order object in the order. This object can be passed around in the program, just as an order can be passed from the waiter to the chef. In this way, the guest does not need to know the name of the cook, thus unlocking the coupling relationship between the caller and the receiver of the request.

Third, the advantages and disadvantages of command mode

Advantages:

Decoupling between classes: there is no dependency between the caller role and the receiver role. The caller only needs to call the execute method of the Command abstract class when implementing the function without knowing which receiver executes it. Extensibility: subclasses of Command can be easily extended, while caller Invoker and high-level module Client do not produce strict code coupling. Command mode combined with other modes will be better: command mode can be combined with responsibility chain mode to achieve the task of command family parsing; combined with template method mode, it can reduce the expansion of Command subclasses.

Disadvantages:

Command mode is also flawed, please see the subclass of Command: if there are N commands, the problem will come out, the subclass of Command is not a few, but N, this class is very inflated, this requires the reader to carefully consider the use of this item.

Fourth, the realization of command mode.

The Receiver class, which knows how to implement and perform an operation related to a request, any class can act as a recipient

Class Receiver {public void Action () {Console.WriteLine ("execute request!") ;}}

The Command class, which is used to declare the interface that performs the operation

Abstract class Command {protected Receiver receiver; public Command (Receiver receiver) {this.receiver = receiver;} abstract public void Execute ();}

The ConcreteCommand class, which binds a recipient object to an action and invokes the corresponding operation of the recipient to implement Execute.

Class ConcreteCommand: Command {public ConcreteCommand (Receiver receiver): base (receiver) {} public override void Execute () {receiver.Action ();}

The Invoker class, which requires the command to execute the request

Class Invoker {private Command command; public void SetCommand (Command command) {this.command = command;} public void ExecuteCommand () {command.Execute ();}}

Client code

Static void Main (string [] args) {Receiver r = new Receiver (); Command c = new ConcreteCommand (r); Invoker I = new Invoker (); i.SetCommand (c); i.ExecuteCommand (); Console.Read ();} so far, I believe you have a deeper understanding of "how to understand the command pattern of Java design pattern". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report