In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to achieve the command pattern in the Java design pattern". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to achieve the command pattern in the Java design pattern" can help you solve your doubts.
Personal understanding: separate multiple commands in a class, put a command in each class to achieve decoupling, a class corresponds to only one function, and all commands are managed by another class when using commands.
Disadvantages: too many functions will result in too many classes created
Command pattern (Command Pattern) is a kind of data-driven design pattern, which belongs to behavioral pattern. The request is wrapped in the object in the form of a command and passed to the callback object. The callback object finds a suitable object that can handle the command, and passes the command to the corresponding object, which executes the command.
Introduction
Intention: encapsulate each request into an object and parameterize the customer from the request that enables you to enter different requests.
The main solution: in the software system, there is usually a tightly coupled relationship between the requester and the implementer, but in some cases, if it is necessary to record, undo or redo, transactions, and so on, this method is not suitable for the tightly coupled design of resisting changes.
When to make changes: in some situations, it is not appropriate to resist the tight coupling of changes, such as recording, undoing / redoing, transactions, and so on. In this case, how to decouple "being a requestor" and "being an implementer"? By abstracting the "group" as an object, the loose coupling between users can be realized.
How to solve it: call the recipient to execute the command through the mediator, in the order: the mediator → command → the recipient.
Key code: separate the commands in the class to create classes for them, and these command classes have the same parent class.
Advantages:
The degree of coupling is reduced.
New commands can be easily added to the system.
Cons: making the declare command mode may result in some systems having too many specific command classes.
Make scene: any ground that is considered a command can be in command mode, for example, every button in 1.GUI is a command. two。 Simulate CMD.
Note: the system requires undo (Undo) and restore (Redo) operations of holding commands, or you can consider making the cancel command mode.
Realize
We first create a receiving Order as a command, and then create a Stock class as a request. Entity command classes BuyStock and SellStock, which implement the Order connection, will be responsible for the actual command processing. Create a class Broker as a callback object that accepts orders and can place orders. The Broker object makes the slave command mode, which determines which object executes which command based on the type of command. The CommandPatternDemo class uses the command Broker class to demonstrate command mode.
Specific implementation steps
1. Create an Java item.
two。 Create a stock request class Stock.
Package src.com. Design pattern. Command mode; / * * stock class * * / public class Stock {private String name; private int quantity; public Stock (String name, int quantity) {this.name = name; this.quantity = quantity;} public void buy () {System.out.println ("stock purchase success" + name+ ", number of shares:" + quantity) } public void sell () {System.out.println ("share sold successfully" + name+ ", number of shares:" + quantity);}}
3. Create a command to pick up Order.
Package src.com. Design pattern. Command mode; public interface Order {void execute ();}
4. Create an entity class BuyStock that implements Order connection.
Package src.com. Design pattern. Command mode; public class BuyStock implements Order {/ / depends on the Stock object private Stock stock; public BuyStock (Stock stock) {this.stock = stock;} @ Override public void execute () {/ / the business operation of purchasing the target stock stock.buy ();}}
5. Create an entity class SellStock that implements Order connection.
Package src.com. Design pattern. Command mode; public class SellStock implements Order {private Stock stock; public SellStock (Stock stock) {this.stock = stock;} @ Override public void execute () {stock.sell ();}}
6. Create the command callback class Broker.
Package src.com. Design pattern. Command mode; import java.util.ArrayList;import java.util.List;/** stockbroker Human * * / public class Broker {private List orderList = new ArrayList (); / / 1. Accept the order public void takeOrder (Order order) {orderList.add (order);} / / 2. Execute order public void placeOrders () {orderList.forEach (fun-> {fun.execute ();});}}
7. Create the CommandPatternDemo class, and then have the listen Broker class accept and execute the command.
Package src.com. Design pattern. Command mode; public class CommandPatternDemo {public static void main (String [] args) {Stock stock = new Stock ("002607", 100); BuyStock buyStock = new BuyStock (stock); SellStock sellStock = new SellStock (stock); Broker broker = new Broker (); broker.takeOrder (buyStock); broker.takeOrder (sellStock); broker.placeOrders ();}}
8. Execute the program and output the results.
After reading this, the article "how to realize the Command pattern in Java Design pattern" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.