Collection of design pattern articles: http://aphysia.cn/categories/designpattern
Start with a picture, and the rest is all up to writing...
introduce
Adapter mode (Baidu Encyclopedia): In computer programming, the adapter mode (sometimes called packaging style or packaging) adapts the interface of a class to what the user expects. An adaptation allows classes that normally cannot work together because of interface incompatibility to work together, by wrapping the class's own interface in an existing class.
The main purpose of the adapter mode is to make compatible with . The original two classes or interfaces that do not match can work together. It belongs to the structural mode and is mainly divided into three types: class adapters, object adapters, and interface adapters.
The adaptor mode is more flexible and can improve reusability, but if abused, the system call relationship will be more complicated. is essentially a compromise .
Constantly compromise, and finally usher in, must be reconstruction.
Adapter mode type
Class adapter
Description: The adapter class ( Adapter
) inherits the original class and realizes the target interface at the same time. The completed function is to have the attribute methods of the original class and call the target interface at the same time.
Example: One type of charger (target type) can IPhone
, another type of charger (interface) can Android
mobile phone, we want to implement an adapter that allows IPhone
charger to have the function of Android
The code structure is as follows:
AndroidCharger.class
:
//给android充电的接口
public interface AndroidCharger {
public void androidout();
}
AppleCharger.class
//给苹果充电的类 public class AppleCharger { public void iphoenOut(){ System.out.println("我是充电器,我可以给苹果充电..."); } }
ChagerAdapater.class
//充电适配器 public class ChagerAdapater extends AppleCharger implements AndroidCharger { @Override public void androidout() { iphoenOut(); System.out.println("适配器开始工作----"); System.out.print("我拥有了给Android充电的能力"); } }
Test.class
public class Test { public static void main(String[]args){ ChagerAdapater chagerAdapater = new ChagerAdapater(); chagerAdapater.androidout(); } }
The results of the operation are as follows:
Personal understanding: The reason why one inherits one interface here is because Java can only inherit from one another. To adapt to multiple classes, only one can inherit and one can be implemented by an interface, which has certain limitations. The method of rewriting it is also more flexible, and the interface method can be modified.
2. Object adapter
Personal understanding: The above-mentioned class adapter is realized by inheriting and implementing the interface (the inherited parent class and interface are all class
), the object adapter is based on the "synthetic reuse principle", does not use the inheritance relationship , Instead, the association relationship , and the object of another class is directly regarded as the member object , which is the instance of the class that needs to be inherited before.
The code structure has not changed, only a package has been recreated:
ChagerAdapater.class
after the change//充电适配器 public class ChagerAdapater implements AndroidCharger { //持有苹果充电器的实例 private AppleCharger appleCharger; //构造器 public ChagerAdapater(AppleCharger appleCharger){ this.appleCharger = appleCharger; } @Override public void androidout() { System.out.println("适配器开始工作----"); System.out.print("我拥有了给Android充电的能力"); } }
Changed Test.class
public class Test { public static void main(String[]args){ ChagerAdapater chagerAdapater = new ChagerAdapater(new AppleCharger()); chagerAdapater.androidout(); } }
The result of the operation has not changed:
- Personal understanding: This is the same as the first type of adapter in thought, but the way it is implemented is different. The class adapter implements interfaces through inherited classes. The object adapter turns the class to be inherited into a attribute object . Associated with the adapter, that is, the adapter's class holds the object instance of the original parent class. Generally speaking, because
java
is a single inheritance, we try not to waste the opportunity to use inheritance this time, so writing is more flexible.
3. Interface adapter
The interface adapter can also be called the default adapter mode or the default adapter mode. When we do not need to implement all the methods implemented by the interface, we can design an abstract class to implement the interface, and then provide a default implementation for all methods in this abstract class, and subclasses of this abstract class can selectively The method is implemented.
The code structure is as follows:
Explained: student class can eat, learn, but
teachers also like to eat, but the teacher is not to learn, but to teach, so we put
learning,
eat,
method of teaching as an interface, since not all classes need to implement For all interfaces, we have implemented an abstract class in the middle to implement these interfaces, and all methods provide a default implementation method. Then the student class or the teacher class inherits the abstract class, so as to realize some of the methods they need.
myInterface.class
//定义接口的方法
public interface myInterface {
//学习的接口方法
public void study();
//教书的接口方法
public void teach();
//吃饭的接口方法
public void eat();
}
myAbstractClass.class (Abstract Class)
public abstract class myAbstractClass implements myInterface{ //学习的接口方法 @Override public void study(){} @Override //吃饭的接口方法 public void eat(){} //教书的接口方法 @Override public void teach(){} }
Student.class (student class)
public class Student extends myAbstractClass{ //学习的接口方法 @Override public void study(){ System.out.println("我是学生,我要好好学习"); } @Override //吃饭的接口方法 public void eat(){ System.out.println("我是学生,我要吃饭"); } }
Teacher.class (teacher class)
public class Teacher extends myAbstractClass { @Override //吃饭的接口方法 public void eat(){ System.out.println("我是教师,我要吃饭"); } //教书的接口方法 @Override public void teach(){ System.out.println("我是教师,我要教育祖国的花朵"); } }
Test.calss (test class)
public class Test { public static void main(String[] args){ Student student = new Student(); Teacher teacher = new Teacher(); student.eat(); student.study(); teacher.eat(); teacher.teach(); } }
The result of the operation:
4. Summary
1. The class adapter mode is mainly to implement the interface method to be added by inheriting the target class, so that the class and the interface can be adapted to work together.
2. The function of the object adapter is roughly the same as that of the class adapter, but in order to be more flexible, the method of inheritance is no longer used, but the method of member variables is directly used to hold the object of the target class, and then realize the interface to be added Method to achieve the same goal.
3. The interface adapter mode is to define all the methods into an interface, then create an abstract class to implement all the methods, and then use the real class to inherit the abstract class. You only need to rewrite the required methods to complete the adaptation. With the function.
If you are interested, you can learn about another way of saying the adapter mode [ https://blog.csdn.net/Aphysia/article/details/80292049]
4. It is recommended to use the adapter mode of the object as much as possible, and use less inheritance. The adapter pattern is also a packaging pattern, which has the same packaging function as the decoration pattern. In addition, the object adapter pattern also has the meaning of delegation. Generally speaking, the adapter mode belongs to the compensation mode, which is specially used for the later expansion and modification of the system, but be careful not to overuse the adapter mode.
[Profile of the author] :
Qin Huai, [161bd47b32181e Qinhuai Grocery Store ], the road to technology is not at a time, the mountains and rivers are long, even if it is slow, it will never stop. Personal writing direction: Java source code analysis,
JDBC
, Mybatis
, Spring
, redis
, distributed,
sword refers to Offer,
LeetCode
, do not like to write a series of articles seriously, do not like to write each article seriously , I cannot guarantee that what I have written is completely correct, but I guarantee that what I have written has been practiced or searched for information. I hope to correct any omissions or errors.
refers to all offer solutions PDF
attention to the public account "Qin Huai Grocery Store" to receive the PDF
Offer V1 version. The V2 version has added questions. It is still in the update of hum and hum, and the C++
added for each problem, so stay tuned .
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。