In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the classes in the web intermediary mode, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use the classes in the web intermediary mode. Let's take a look.
Effect.
Using intermediary mode, you can:
It is beneficial to extract the common functions that can be reused. All colleague classes only need to interact with the intermediary class at the information exchange center without knowing the existence of other controls. This not only reduces the communication channels, but also reduces the coupling between colleagues.
Business logic is easy to understand. Terminator class is not only the center of information exchange, but also the center of information processing. Because all the content related to business logic is concentrated in one place, it is easy to understand and maintain.
Because the intermediary class absorbs all the business logic, the colleague class can focus entirely on its own functions, resulting in the fact that the colleague class can be easily reused.
Code example
The sample code is basically written with reference to the pattern class diagram, and the class name has been changed slightly for ease of understanding. The first is the abstract intermediary class.
Class Mediator (metaclass=abc.ABCMeta): @ abc.abstractmethod def notify (self, colleague): raiseNotImplementedError
Notification methods similar to the observer mode are used here for maximum flexibility. Next is the abstract colleague class.
Class Colleague: def _ _ init__ (self,mediator): self.mediator= mediator
The abstract colleague class manages an object of a mediator class that initiates communication with the object class.
Class TextEntry (Colleague): def _ init__ (self,mediator): Colleague.__init__ (self,mediator) self.text= "" def set_text (self, text): self.text= text print ('TextEntry:', self.text) class ListBox (Colleague): def _ init__ (self,mediator): Colleague.__init__ (self) Mediator) self.index= 0 defitem_selected (self): self.index= self.index + 1 self.mediator.notify (self) def get_selected (self): returnself.index
The only function of the sample code is to represent the option index in the TextEntry when the current option of the ListBox changes, so the method of implementation is limited to the necessary parts of the function.
Class ConcreteMediator (Mediator): def _ init__ (self): self.lb= ListBox (self) self.te= TextEntry (self) def notify (self, colleague): if colleague== self.lb: self.te.set_text (self.lb.get_selected () def test (self): self.lb.item_selected ()
If _ _ name__ = ='_ main__': mediator = ConcreteMediator () mediator.test ()
In the main function, we first build an instance of the intermediary class ConcreteMediator. In the initialization process, ConcreteMediator builds objects of the ListBox class and the TextEntry class with itself as parameters. After this step is completed, all the preparatory work will be ready.
The next step is to call the item_selected method of ListBox through the test of the mediation class ConcreteMedator. In actual development, this call should originate from a user action. In ListBox's item_selected method, ListBox first determines its current activity based on internal logic, and then calls the notification interface notify of the abstract mediator class to report its changes.
The intermediary class is at the center of the information exchange, receiving the information from the action control and then sending the processing result to the necessary receiver. Specific to the sample code, the Mediator class simply sets the index of the active item to the TextEntry.
This is the end of the article on "how to use classes in the web Mediator pattern". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use classes in web intermediary mode". If you want to learn more, 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.