23 design patterns (5) Java adapter pattern
23 design patterns Part 5: Java adapter pattern
definition:
Convert the interface of a class into another interface that the customer wants. The adapter pattern allows classes that cannot work together because of interface incompatibility to work together.
Role:
Target role: This is the expected interface, that is, this kind 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 we need to adapt now.
Adapter role: the adapter class is the core of the adapter pattern. The adapter converts the source interface to the target interface. Obviously, this role cannot be an interface, but must be a concrete class.
Classification:
1. Class adapter mode
Now you want to implement the target interface, but you don't want to refactor. If you want to use the existing Adaptee class, you can define an adapter class, inherit the class you want to use, and implement the expected interface.
In this way, the plan is completed by using the adapter class and implementing the target interface. Test:
2. Object adapter mode
The adapter class is associated with the existing Adaptee class and implements the standard interface. The advantage of this is that inheritance is no longer required.
We can imagine that the output result is the same as the class adapter mode. Test:
difference:
The adapter pattern of the object is not connected to the Adaptee class using inheritance, but to the Adaptee class using delegation.
advantage:
Reusability
The system needs to use existing classes, and such interfaces do not meet the needs of the system. Then these functions can be reused better through the adapter mode.
Expansibility
When implementing the adapter function, you can freely call the functions developed by yourself, so as to naturally expand the functions of the system.
Disadvantages:
Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, you can clearly see that the a interface is called, but in fact, it is internally adapted to the implementation of the B interface. Therefore, the adapter mode is not suitable for use in the detailed design stage. It is a compensation mode, which is specially 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 so that it can work with other unrelated classes or unforeseen classes; 3. Use some existing subclasses without subclassing them to match the interface. 4. The old system developed classes have implemented some functions, but the client can only access them in the form of other interfaces, but we don't want to change the original classes manually.
Summary:
The adapter mode is not suitable for use in the detailed design stage. It is a compensation mode, which is specially used in the later expansion and modification of the system. The adapter mode is more like a remedial measure.
Transferred from: Java confidant
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.