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 relevant knowledge of "how to implement the adapter pattern of java". 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!
I. Model motivation
The use of design and coding techniques similar to power adapters in software development is called adapter patterns.
Typically, the client can access the services it provides through the interface of the target class. Sometimes, the existing class can meet the functional needs of the client class, but the interface it provides may not necessarily be expected by the client class, which may be caused by the inconsistency between the legal name in the existing class and the method name defined in the target class.
In this case, the existing interface needs to be transformed into the interface expected by the client class, which ensures the reuse of the existing class. Without this transformation, the client class cannot take advantage of the functionality provided by the existing class, and the adapter pattern can do so.
In the adapter pattern, you can define a wrapper class that wraps objects with incompatible interfaces. This wrapper class refers to the adapter (Adapter), and the object it wraps is the Adaptee, that is, the adapted class.
The adapter provides the interface required by the client class, and the implementation of the adapter is to convert the request of the client class into a call to the corresponding interface of the adaptor. In other words, when the client class calls the method of the adapter, the method of the adaptor class will be called inside the adapter class, and the process is transparent to the client class, which does not directly access the adaptor class. Therefore, the adapter enables classes that cannot interact because of interface incompatibility to work together. This is the pattern motivation of the adapter pattern.
II. Pattern definition
Adapter pattern (Adapter Pattern): converts one interface to another that the customer wants, and the adapter pattern allows classes with incompatible interfaces to work together, aliased as Wrapper. The adapter pattern can be used as either a class structural pattern or an object structural pattern.
III. Pattern structure
The adapter pattern contains the following roles:
Target: target abstract class
Adapter: adapter class
Adaptee: adaptor class
Client: customer CLA
The adapter pattern has two implementations: object adapter and class adapter:
3.1. Object Adapter:
Image
3.2. Class adapter:
Image
Fourth, code analysis 4.1, object adapter code implementation public interface Target {
Void sampleOperation1 ()
Void sampleOperation2 ();}
Public class Adaptee {
Public void sampleOperation1 () {System.out.println ("sampleOperation1");}}
Public class Adapter implements Target {
Private Adaptee mAdaptee
Public Adapter (Adaptee adaptee) {mAdaptee = adaptee;}
@ Override public void sampleOperation1 () {mAdaptee.sampleOperation1 ();}
@ Override public void sampleOperation2 () {System.out.println ("sampleOperation2");}}
Public class MyClass {
Public static void main (String [] args) {Adapter adapter = new Adapter (new Adaptee ()); adapter.sampleOperation1 (); adapter.sampleOperation2 ();}}
The output is as follows:
SampleOperation1sampleOperation24.2, class adapter code implementation public interface Target {
Void sampleOperation1 ()
Void sampleOperation2 ();}
Public class Adaptee {
Public void sampleOperation1 () {System.out.println ("sampleOperation1");}}
Public class Adapter extends Adaptee implements Target {
@ Override public void sampleOperation2 () {System.out.println ("sampleOperation2");}}
Public class MyClass {
Public static void main (String [] args) {Target adapter = new Adapter (); adapter.sampleOperation1 (); adapter.sampleOperation2 ();}}
The output is as follows:
SampleOperation1sampleOperation2 V. Advantages
Decouple the target class from the adaptor class and reuse the existing adaptor class by introducing an adapter class without modifying the original code.
It increases the transparency and reusability of the class, encapsulates the specific implementation in the adaptor class, which is transparent to the client class, and improves the reusability of the adaptor.
Flexibility and expansibility are very good, through the use of configuration files, adapters can be easily replaced, and new adapter classes can be added without modifying the original code, which is fully in line with the "opening and closing principle".
The class adapter pattern also has the following advantages:
Because the adapter class is a subclass of the adaptor class, some adaptor methods can be replaced in the adapter class, making the adapter more flexible.
The object adapter pattern also has the following advantages:
An object adapter can adapt multiple different adapters to the same target, that is, the same adapter can adapt both the adaptor class and its subclasses to the target interface.
6. The disadvantages of the class adapter pattern are as follows:
For languages that do not support multiple inheritance, such as Java and C#, there can only be one adaptor class at a time, and the target abstract class can only be an abstract class, not a concrete class. Its use has some limitations, so it is impossible to adapt an adaptor class and its subclasses to the target interface.
The disadvantages of the object adapter pattern are as follows:
Compared with the class adapter pattern, it is not easy to replace the methods of the adaptor class. If one or more methods of the adaptor class must be replaced, we have to first be a subclass of the adaptor class, replace the methods of the adaptor class, and then adapt the subclass of the adaptor class as a real adaptor. the implementation process is more complicated.
VII. Applicable environment
Adapter mode can be used in the following situations:
The system needs to use existing classes, and the interfaces of these classes do not meet the needs of the system.
You want to create a reusable class to work with classes that are not much related to each other, including classes that may be introduced in the future.
VIII. Mode application
In 1996, Sun disclosed the database connection tool JDBC,JDBC of Java language, which enables Java language programs to connect with databases and use SQL language to query and manipulate data. JDBC provides a general abstract interface for the client. The JDBC driver software of each specific database engine (such as SQL Server, Oracle, MySQL, etc.) is an adapter software between the JDBC interface and the database engine interface. The corresponding adapter software is needed between the abstract JDBC interface and each database engine API, which is the driver for each different database engine.
IX. Pattern expansion
Default Adapter Pattern mode or default adapter mode
When you do not need to implement all the methods provided by the interface, you can first design an abstract class implementation interface and provide a default implementation (empty method) for each method in the interface. then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements, which is suitable for situations where an interface does not want to use all its methods. Therefore, it is also called single-interface adapter pattern.
This is the end of the content of "how to implement the adapter pattern of java". 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.