In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to implement the java adapter pattern". 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!
Definition:
Convert the interface of one class to another interface that the customer wants. The adapter pattern allows classes that cannot work together because the interfaces are not compatible.
Roles:
Target role: this is the expected interface, that is, this type of interface meets our requirements.
Adapee role: the interface we want to use, but this interface does not meet our requirements, that is, the interface that needs to be adapted now.
Adaper role: the adapter class is the core of the adapter pattern. The adapter converts the source interface to the destination interface. Obviously, this role cannot be an interface, but must be a concrete class.
Classification:
1, class adapter pattern
Class Adaptee {
Publicvoid specificRequest () {System.out.println ("Special request, this is the source role");}}
/ * this is the target role, the expected interface * /
Interface Target {
Publicvoid request ();}
Now that you want to implement the Target interface, but don't want to ReFactor, and want to use the existing Adaptee class, you can define an adapter class, inherit the class you want to use, and implement the desired interface.
Class Adapter extends Adaptee implementsTarget {publicvoid request () {super.specificRequest ();}}
In this way, using the adapter class and implementing the target interface completes the planning and testing:
Public class Test {publicstatic void main (String [] args) {/ / use the special function class, that is, the adaptation class Targetadapter = new Adapter (); adapter.request ();}}
2, object adapter pattern
The adapter class associates existing Adaptee classes and implements the standard interface, which has the advantage that inheritance is no longer required.
Class Adapter implements Target {privateAdaptee adaptee; publicAdapter (Adaptee adaptee) {this.adaptee= adaptee;} publicvoid request () {this.adaptee.specificRequest ();}}
We can imagine that the output at this point is the same as the class adapter pattern, testing:
Public class Test {publicstatic void main (String [] args) {Targetadapter = new Adapter (new Adaptee ()); adapter.request ();}}
Difference:
Instead of using inheritance relationships to connect to the Adaptee class, the adapter pattern for the object uses delegate relationships to connect to the Adaptee class.
Advantages:
Reusability
The system needs to use an existing class, and the interface of this class does not meet the needs of the system. Then these functions can be better reused through the adapter pattern.
Expansibility
When implementing the function of the adapter, you can freely call the function developed by yourself, so as to naturally expand the function of the system.
Disadvantages:
Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that interface An is called, but in fact, it is internally adapted to the implementation of interface B. Therefore, the adapter pattern is not suitable for use in the detailed design phase, it is a compensation mode, dedicated to be used in the later expansion and modification of the system.
Applicable scenarios:
1. The interfaces of existing classes do not meet our requirements.
2. Create a reusable class that works with other unrelated or unforeseen classes
3. Use some existing subclasses without subclassing to match the interface.
4. The class developed by the old system has implemented some functions, but the client can only access it in the form of another interface, but we do not want to change the original class manually.
That's all for "how to implement the java Adapter pattern". 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.