Adapter Mode (Adapter)

Source: Internet
Author: User

Principle: Convert the interface of a class into another interface that the customer wants. The adapter mode allows classes that are incompatible with interfaces to work together. Role: Target abstract class (target): the interface that the customer expects.         The target can be a specific or abstract class, or it can be an interface.         Adapter Class (Adaptee): A class or adapter class that needs to be adapted. Adapter Class (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be adapted. Implementation: Class Adapter mode: The adapter class implements the target abstract class interface and inherits the class of the adapter class, invoking the corresponding method in the inherited ligand class in the request () method of the adaptor classes.The adapter is an inheritance relationship. Object Adapter Pattern: The adapter class inherits the target abstract class and wraps an instance of the adapter, which is an association relationship with the adaptor.     public interface Target {     public void request ();} public class Adaptee {     public void specialrequest () {      System.out.println ("This is special request");      }} //class Adapter public class Classadapter Extends Adaptee implements target{      @Override      public void Request () {          specialrequest ();      }}// Object adapter public class Objectapdater implements Target{     adaptee Adaptee = new Adaptee ();       @Override      public void request () {           adaptee.specialrequest ();     }}   Default adapter Mode: (Single-Interface adapter mode) When you do not need to implement all the methods provided by an interface, you can design an abstract class to implement the interface, and provide a default implementation (empty method) for each method in the interface, then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements. It is suitable for applications that do not want to use an interfaceAll methods of the case.   Bidirectional Adapter: The adapter contains a reference to both the target class and the adapter class through which the target class can invoke methods in the target class.

Adapter mode (Adapter)

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.