The following figure shows the class adapter and Object Adapter. The adapter mode is used to solve the problem of using incompatible interfaces. The following shows that the two adapters have three classes: Target, adaptee, and adapter.
The client wants to use target. Request ()
The existing classes use adaptee. specificrequest ()
Request () and specificrequest () may be returned, and the parameter list is different.
Both the class adapter mode and the Object Adapter mode are converted by grafting an adapter.
However, the two grafting modes are different.
The class adapter mode adopts the multi-inheritance mode.
Class adapter extends adaptee implements target {
Request () {This. specificrequest ();}
....
}
The Object Adapter adopts the include method.
Class adapter implements taget {
Adaptee;
Request () {adaptee. specificrequest ();}
...
}
I think there are the following differences between the two:
1. You need to create an adaptee for the class adapter mode,
The Object Adapter mode can directly use an existing adaptee instance to convert the interface.
2. The class adapter inherits adaptee, so you can extend specificrequest () through overwriting ()
The Object Adapter and adaptee cannot be extended because of the Inclusion relationship. (For details, see the following section)
3. The class adapter mode is relatively static because it is inherited, and the Object Adapter mode is relatively flexible because it contains a combination (you can write the adaptee subclass Extension function)
Class Adapter
Object Adapter
Purpose: Convert the interface of a class to another interface that the customer wants. This mode allows classes that cannot work together due to incompatibility of interfaces to work together.