Adapter mode: Class adapter and Object Adapter

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.