In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of adapter patterns in Java, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Adapter pattern of Java programming pattern series
Understanding adapter design patterns requires connections from scenarios in life. What can be called adapters in life? Literally, "fit" means pairing one thing with another so that they can work together. For example, people's laptops need to use a power adapter to switch sockets and currents to the laptop to power the laptop. For example, all the electrical sockets in China are two-or three-port domestic standard sockets, but if you travel to areas other than Chinese mainland and want to use power to power electronic products, you need to change the sockets. For example, when Mr. Wang arrived in Hong Kong, he saw an electrical outlet like this:
It is obvious that mobile phones brought from China cannot be recharged.
So how to solve this problem?
After searching for information, teacher Wang found a product that looks like this, called "power adaptor".
I believe the usage will be clear at a glance.
With it, mom doesn't have to worry about me anymore. I'm out of electricity.
"Oh! I see. Isn't this a transition port?" Yes, in fact, it is the same as the concept of a card reader, which is to convert an unusable interface into a usable interface through an adapter.
Concept:
Adapter pattern (Adapter): converts the interface of one class into another interface that the customer wants. The adapter pattern allows classes that cannot work together because the interfaces are not compatible.
"I still don't understand it!" Never mind, here is an easy-to-understand case + code to let you know what an adapter pattern is.
Next, let's use the power supply case to introduce the adapter pattern:
There are two incompatible interfaces, power An interface and power B interface, and have their own corresponding implementation methods. The code is as follows:
Then the two interfaces should have their own implementation classes, with the following code:
Next, it should be the client to call, first define the power supply A to start working method:
So if you want to call the start method, you need to pass in the implementation class object of PowerA.
So here we create a PowerAImpl object and call the start method.
The result of running after compilation is as follows:
We've already written it here. If we want to pass the PowerB interface to the start method at this time, can we? What should I do?
So at this time, we have two ways to achieve this, the first one:
Add another startB method, but this is not a good solution, because there may be some logic and business before and after calling the insert method in startA, and there may be the same logic and business before and after calling the connect method in startB, only the connect method call is different, so rewriting a copy is redundant and the code is repeated. What should we do if it doesn't quite conform to our object-oriented programming idea? Look down.
Next, let's take a look at the second solution: we still want to use a start method to solve this problem. Here, some people will think of directly changing the start method to adapt to the two interfaces. Teacher Wang needs to explain a problem here. There is a very important point in the principle of object-oriented programming that is "closed for modification and open for extension". If our program is already running online. At that time, it was not considered to send the PowerB interface, only the PowerA interface, but now the business modification requirements can also be sent to the PowerB interface, so the impact of the modification method on other written programs is also unknown. There may be a problem when other partners call your start method, that is to say, the code that calls your start method will have a chain reaction. OK! Look down there!
We need to achieve something like this.
Obviously, this kind of code compilation will not pass, because at present, PowerA and PowerB are already two different interfaces, which is the same as a three-port socket and a two-port plug.
To achieve the transformation, you now need an adapter! We need to convert PowerB to PowerA and pass it in, so we make an adapter for PowerA.
1. The first thing to adapt is PowerA, so implement the PowerA interface, so PowerAAdapter can be passed into the start method as an implementation class of PowerA.
2. If we want to use this adapter to convert PowerB to PowerA, we need to get in touch with PowerB, so define a PowerB as a property and initialize it through the constructor.
3. After implementing the PowerA interface, you need to override its method, so call the connect method of PowerB in the rewritten insert method, and the adapter will write it.
Now let's test it:
The result of running after compilation is as follows:
The results show that the implementation class of the PowerB interface starts to work, but the parameters are passed in the form of the PowerA interface. This is the magic adapter pattern!
Follow the steps and knock on the code! It's more helpful to understand!
The above is all the content of the article "sample Analysis of Adapter patterns in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.