Java Adapter Mode
Recently, I have been learning Java basics and have a lot of doubts when I was learning adapters. I searched the information online and found a lot of information about Adapter, but this article is good. Here is a record, you can read what you need.
The adapter pattern is to convert the interface of one class into another interface that the client expects, so that two classes whose original interfaces do not match and cannot work together can work together. Functionally speaking, classes that are incompatible with these interfaces generally have the same or similar functions. Usually we solve this kind of interface incompatibility by modifying the interface of this class, but if we are unwilling to modify the original interface for an application, or we don't have the source code of the object at all, the adapter mode will come in handy.
Advantages of Adapter:
1. Decouple the target class and the adapter class
2. Increase the transparency and reusability of the class, encapsulate the specific implementation in the adapter class, which is transparent to the client class, and improves the reusability of the adapter.
3. Both flexibility and expansion are very good, and they conform to the principle of opening and closing
The roles involved in the adapter include the following:
Target: Defines the specific interface used by a client.
Client: Use the target interface to cooperate with objects consistent with the target interface.
Adaptee: An existing interface that needs to be adapted.
Adapter: Responsible for converting Adaptee's interface to Target's interface. Adapters are a concrete class, which is the core of this pattern.
Adapters are divided into two types: class adapter and object adapter, which will be described in detail below.
Class adapter
The so-called class adapter refers to the adapter Adapter inheriting our adapter Adaptee and implementing the target interface Target. Since it is single inheritance in Java, this adapter can only serve the inherited adapter Adaptee. The code is as follows:
Adaptee
package com.bluemsun.classadapter;public class Person { private int id; private String name; /** * person can only speak English now*/ public void saysEnglish(){ System.out.println("Person can say english!"); } /** * Omit setter,getter. */}Target interface (Target)
package com.bluemsun.classadapter;/** * The target requires that person can speak English, French, and Japanese. But now person can only speak English* @author Administrator * */public interface Target_Person { void saysEnglish(); void saysFrench(); void saysJapanese();}Adapter
package com.bluemsun.classadapter;/** * Class adapter, because it inherits Person, and Java can only inherit in single, so this adapter only serves the person class* This adapter allows the person class to implement the methods specified by the target interface without modifying the source code* @author Administrator * */public class Adapter_Person extends Person implements Target_Person{ @Override public void saysFrench() { System.out.println("Person can say French!"); } @Override public void saysJapanese() { System.out.println("Person can say Japanese!"); }}Client
package com.bluemsun.classadapter;public class Test { public static void main(String[] args) { Target_Person person = new Adapter_Person(); person.sayEnglish(); person.sayFrench(); person.sayJapanese(); }}The above few simple codes demonstrate the role of class adapters. As we said at the beginning, this adapter Adapter can only serve the Person class. At this time, you may wonder, if I need to adapt to many classes, do I need to write an Adapter for each class that needs to adapt to? Is there a more flexible way? The answer is: Yes! This is the object adapter we are talking about below.
Object Adapter
The so-called object adapter, simply put, means that the adapter implements our target interface, but does not inherit the classes that need to be adapted. Instead, it is adapted by passing in the adapter's constructor. The code is as follows: (Target, Adaptee Same as above)
Adapter
package com.bluemsun.objectdapter;import com.bluemsun.classadapter.Person;import com.bluemsun.classadapter.Target_Person;/** * Object adapter, unlike class adapters, the object adapter can adapt multiple sources to the target* @author Administrator * */public class Adapter_Person implements Target_Person{ //Only implement the target interface private Person person; //Pass the Adaptee class Person in the constructor public Adapter_Person(Person person){ this.person = person; } //Implement sayEnglish() in the target interface--Call sayEnglish() in Adaptee @Override public void sayEnglish() { this.person.sayEnglish(); } //Implement other methods in the interface @Override public void sayFrench() { System.out.println("person can say French!"); } @Override public void saysJapanese() { System.out.println("person can say Japanese!"); }}Client
package com.bluemsun.objectdapter;import com.bluemsun.classadapter.Person;import com.bluemsun.classadapter.Target_Person;public class Test { public static void main(String[] args) { Target_Person person = new Adapter_Person(new Person()); person.sayEnglish(); person.sayFrench(); person.sayJapanese(); }}Object adapters can adapt to multiple classes with adaptations. Just pass different classes with adaptations in Adapter's constructor. Flexibility.
Advantages of Class Adapters:
1. Since the adapter class is a subclass of the adapter class, some adapter methods can be replaced in the adapter class, making the adapter more flexible.
Disadvantages of class adapters:
1. For languages such as Java and C# that do not support multiple inheritance, at most one adapter class can be adapted to at least one adapter class at a time, and the target abstract class can only be an interface, not a class. Its use has certain limitations. It cannot be adapted to an adapter class and its subclass to the target interface at the same time.
Advantages of Object Adapters:
1. Adapt multiple different adapters to the same target, that is, the same adapter can adapt both the adapter class and its subclass to the target interface.
Disadvantages of Object Adapters:
1. Compared with the class adapter mode, it is not easy to replace the adapter class.
Thank you for reading, I hope it can help you. Thank you for your support for this site!