The problem of incompatibility between existing classes and new interfaces is quite common, and one solution has been found for it. This solution is the adapter.
1 What is adapter what
The main function of the adapter is to pass the behavior of the adapter to the client at the other end of the pipeline.
Transforms the interface of one class into another interface that the customer wants. The adapter mode works with those classes that would otherwise not work together because of incompatible interfaces.
There are two ways of implementing an adapter:
1) class Adapter: Two interfaces can be adapted by inheritance. Inherit adaptee, follow <Target> agreement.
2) object adapter: Adapted by a combination. Follow the <Target> agreement, combining a reference to a team adaptee.
Difference:
Class Adapter
Only for a single specific Adaptee class, the Adaptee is adapted to target;
Easy to overload the behavior of Adaptee, because it is through the direct sub-class of adaptation;
There is only one adapter object that accesses adaptee indirectly without additional pointers.
Object Adapter
Can be adapted to a plurality of adaptee and subclasses;
It is difficult to overload the behavior of adaptee, it needs to use the object of subclass rather than adaptee itself;
Additional pointers are required to indirectly access the Adaptee and adapter behavior.
2 When to use adapter mode
1) The interfaces of the existing classes do not match the requirements
2) Want a reusable class that can write to other classes that may have incompatible interfaces
3) It is necessary to fit a few different subclasses of a ray, but to let each subclass subclass an adapter is unrealistic, you can use an object adapter (also called a delegate) to fit the interface of its parent class.
OC Programming Path-Adapter for interface adaptation