Adapter pattern in Java)

Source: Internet
Author: User

1. Concept: convert an interface into another interface that the customer wants. (This mode allows incompatible classes to work together)

2. UML diagram

(1)ObjectAdapterSchema Structure

(2)ClassAdapterSchema Structure


3.Code

(1)Object Adapter

 
Public interface target {public void request ();} public class adaptee {public void specificrequest () {system. Out. println ("this is the original standard interface! ") ;}} Public class adapter implements target {private adaptee adapteet = NULL; Public void request () {adapteet. specificrequest ();}}

(2)Class Adapter

Public interface target {public void request ();} public class adaptee {public void specificrequest () {system. Out. println ("this is the original standard interface! ") ;}} Public class adapter extends adaptee implements target {public void request () {super. specificrequest ();}}

4. Application scenarios

Use the adapter mode in the following situations:

1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.

2. Create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future. These source classes do not necessarily have very complex interfaces.

3. (For object adapters) in the design, you need to change the interfaces of multiple existing sub-classes. If you use the class adapter mode, you need to create an adapter for each sub-class, which is not practical.

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.