In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand command design patterns". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Command mode
We often use this pattern in our daily life, to give a very simple example, for example, we release code. After the release, we found that we accidentally posted a bug. What should we do at this time? It's simple to roll back and roll back the online code to the code before this release. In this way, the changes brought about by our release will be eliminated, and the generation of bug will be avoided.
So, what does it need to do for a publishing system? In fact, there are two functions, one is to release and the other is to roll back. The two operations are reversible to each other, and for its users, they don't care how it is implemented internally, we just need to press the button on the page.
Let's review this process, we click publish, we can release the latest code online. After the release, the problem is found, and then click Roll back, and the system automatically returns to the state before the release. Publishing and rollback are reversible to each other, and when we eliminate bug, click publish again, and we can release the latest code again.
This is what the command pattern does, that is, the encapsulation of do and undo. Let's look at a very simple example of renaming a file. For example, we need to change the name of the files in the system from A.txt to B.txt. This function is very simple, the system provides us with a ready-made function called os.rename (), we only need to pass in the addresses of the An and B files.
What if we find that we have changed the name and changed the wrong name and want to roll back? You will find that we have forgotten the name before we changed it, and we don't know how to roll back. Command mode can be used at this point. Let's look at the code:
Import os class MoveFileCommand: def _ init__ (self, src, dest): self.src = src self.dest = dest def execute (self): self.rename (self.src, self.dest) def undo (self): self.rename (self.dest, self.src) def rename (self, src, dest): print ('renaming from {} to {}' .format (src) Dest)) os.rename (src, dest)
In the execute method, we change the file from src to dest, and if we want to roll back, it will call rename again. Roll back the file name from dest to src. In this way, as a user, you don't have to understand the implementation logic inside api at all, otherwise you need to make a lot of adaptations in order to prevent errors.
Menu item
With command mode, we can encapsulate a layer of ui interaction on the outside, and one of our common UI interactions is buttons. After a button is clicked, a pressed tag appears and what function is implemented. Press the flag again to disappear and the function is turned off.
I picked a random example, such as show minimap,show breadcrumbs in the menu below, which are all such functions. Click on the thumbnail to appear, and then click the thumbnail to disappear.
If you have written a UI page, generally speaking, we will first define a Menu Item class that represents the base class of all the item in the menu. Different options represent different item, and on further analysis, we will find that there are some item that we need to double-click to close, while some item do not. For example, the above item such as Run and Output are all executed one at a time.
Of course, we can treat the Command object described above as an item directly, but this is not conducive to the unity of the whole menu, so we will wrap a layer on the outside. For example, the parent class of all MenuItem should look like this:
Class MenuItemBaseClass: def _ init__ (self): pass def pressed (self): pass def unpress (self): pass
With this base class, we can implement a rollback class, taking the object of command as a class member variable, and then implementing the unpress method in it:
Class RedoableMenu (MenuItemBaseClass): def _ init__ (self, command): self_command = command def pressed (self): self._command.execute () def unpress (self): self._command.undo ()
In this way, our UI is decoupled from command, and if we want to implement different rollback functions, we just need to implement different command creation instances. There is no impact on the overall use of UI, and all classes used in UI components are uniform. It may not be obvious in a weakly typed language like Python, because one of our list says it is the list of the menu base class, but we can install anything. But if it is a strongly typed language, then this abstraction and encapsulation is necessary.
This is the end of "how to understand command Design patterns". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.