In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the Bridge pattern of Design patterns". In the operation of actual cases, many people will encounter such a dilemma, 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!
For instance
The main function of the bridge mode is also to decouple the quantities that will change independently from the whole logic, thus saving us the amount of code. Let's use milk tea to give a simple example.
For milk tea, its raw materials are often relatively simple, that is, sugar, water, tea, milk lid and so on. But the production process is often very different, pearl milk tea may just mix tea and milk with pearls, other milk teas may be completely different.
If we want to use the program to simulate the whole process of making milk tea, we will find it very troublesome if we implement a separate class for each kind of milk tea. Because different milk teas are often just different production methods, but the overall raw materials and processes may be the same. So we just want to be able to separate the production process, at this time we can use bridging mode, to put it bluntly, it is actually very simple, especially in Python.
Code implementation
Here we first release the logic of the subject of milk tea, which we may understand at a glance.
Class BubbleTea: def _ _ init__ (self, ice, sugar, tea, cheese Making_api): self._ice = ice self._sugar = sugar self._tea = tea self._cheese = cheese self._making_api = making_api def no_ice (self): self._ice = 0 def additional_sugar (self): self._sugar + = 5 def additional_cheese (self): self._cheese + = 5 Def prepare (self): self._making_api.make (self._ice Self._sugar, self._tea, self._cheese)
Ice, sugar, tea and cheese here are all ingredients that we add to our daily milk tea. For the production of milk tea, we often put forward some requirements such as adding cheese, de-icing and adding sugar, and we also make them into separate methods, which are also easy to understand.
The only thing to pay attention to here is that the process of making milk tea, that is, the prepare method, is not implemented in the BubbleTea class, but is transmitted from the outside world through making_api. This is the application of our bridge pattern. Since the processing logic is from the outside, it is actually decoupled from the category of milk tea. We can define the implementation of this api outside without any impact. If we want to implement milk tea within the BubbleTea class, either we implement a class for each kind of milk tea, or we make a lot of judgments in it, either of which is obviously not very good, which will lead to a lot of code accumulation and bloated.
Finally, let's take a look at the implementation of making_api and an example of using it:
Class CheeseTeaAPI: def make (self, ice, sugar, tea, cheese): print ('cheese tea! Cheese: {}, bubbles: 5, sugar: {}, tea: {}, ice: {} '.format (cheese, sugar, tea, ice) class BubbleTeaAPI: def make (self, ice, sugar, tea, cheese): print (' bubble tea! Sugar: {}, tea: {}, ice: {} '.format (sugar, tea, ice) if _ _ name__ =' _ _ main__': teas = [BubbleTea (0,5,3,0, BubbleTeaAPI ()), BubbleTea (2,5,2,4, CheeseTeaAPI ())] for tea in teas: tea.no_ice () tea.additional_sugar () tea.prepare ()
If you are still confused, you might as well take a look at the code details and think carefully. Overall, the implementation of the bridge pattern in Python is relatively simple, at least much simpler than in Java.
This is the end of "how to understand the Bridge pattern of Design patterns". Thank you for your 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.