Java Adapter mode (Adapter Mode)

Source: Internet
Author: User

Java Adapter mode (Adapter Mode)
Adapter mode definition: Used together two incompatible classes. It is a structural mode and requires two identities: Adaptee and Adaptor. Why do we often encounter the combination of two unrelated classes when using the adapter mode? The first solution is to modify the interfaces of their classes, but if we do not have the source code, or, we do not want to modify interfaces for an application. What should I do?

Use the Adapter to create a Hybrid Interface (mixed blood) between the two interfaces ). How to Use the Adapter mode to implement the Adapter mode, as mentioned in the "think in Java" "class regeneration" section, there are two methods: composition and inheritance ),

Suppose we want to Piling, there are two types: Square Pile round pile. Public class SquarePeg {
Public void insert (String str ){
System. out. println ("SquarePeg insert ():" + str );
}
}

Public class RoundPeg {
Public void insertIntohole (String msg ){
System. out. println ("RoundPeg insertIntoHole ():" + msg );
}
} Now there is an application that requires both square and circular piles. Then we need to integrate these two unrelated classes. If RoundPeg does not have the source code, or we do not want to modify the source code, we will use the Adapter to implement this application: public class PegAdapter extends SquarePeg {
Private RoundPeg roundPeg;
Public PegAdapter (RoundPeg peg) (this. roundPeg = peg ;)
Public void insert (String str) {roundPeg. insertIntoHole (str );}
} In the above Code, RoundPeg belongs to Adaptee and is an adapter. The PegAdapter is an Adapter that adapts Adaptee (RoundPeg) and Target (Target SquarePeg. In fact, this is a comprehensive application of the composition and inheritance methods.

PegAdapter first inherits SquarePeg, then uses the new combination to generate the RoundPeg object roundPeg, and then reloads the parent insert () method. From here, you also know the differences between generating objects using new and using extends to inherit the generated objects. The former does not need to be modified on the original class, or even has no need to know its internal structure and source code.

If you have some experience using Java, we have found that this mode is often used. We further use the above PegAdapter to inherit SquarePeg. If we need to inherit both sides, that is, inherit SquarePeg and inherit RoundPeg, because many inheritance are not allowed in Java, but we can implement (implements) two interfaces ):

  • Public interface IRoundPeg {
    Public void insertIntoHole (String msg );
    } Public interface ISquarePeg {
    Public void insert (String str );
    }
    The following are the new RoundPeg and SquarePeg. Apart from implementing the interface, there is no difference with the above. Public class SquarePeg implements ISquarePeg {
    Public void insert (String str ){
    System. out. println ("SquarePeg insert ():" + str );
    }
    }

    Public class RoundPeg implements IRoundPeg {
    Public void insertIntohole (String msg ){
    System. out. println ("RoundPeg insertIntoHole ():" + msg );
    }
    } The following is a new PegAdapter called two-way adapter: public class PegAdapter implements IRoundPeg, ISquarePeg {
    Private RoundPeg roundPeg;
    Private SquarePeg squarePeg;

    // Constructor
    Public PegAdapter (RoundPeg peg) {this. roundPeg = peg ;}
    // Constructor
    Public PegAdapter (SquarePeg peg) (this. squarePeg = peg ;)

    Public void insert (String str) {roundPeg. insertIntoHole (str );}
    } There is also Pluggable Adapters, which can dynamically obtain one of several adapters. Using Reflection technology, you can dynamically discover Public methods in the class.



    Adapter ModeOverview
    Converts an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.
    Applicability
    1. You want to use an existing class, and its interface does not meet your needs. 2. You want to create a reusable class that can work collaboratively with other unrelated classes or unforeseen classes (that is, classes that may not be compatible with certain interfaces. 3. (applies only to object adapters) You want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces. The Object Adapter can adapt to its parent class interface.
    Participants
    1. Target defines the interfaces used by the Client in specific fields. 2. Collaboration between the Client and objects that comply with the Target interface. 3. Adaptee defines an existing interface, which must be adapted. 4. the Adapter adapts the Adaptee interface to the Target interface.
    Class Diagram ExampleTarget
    public interface Target {    void adapteeMethod();        void adapterMethod();}
    Adaptee
    public class Adaptee {    public void adapteeMethod() {        System.out.println("Adaptee method!");    }}
    Adapter
    public class Adapter implements Target {    private Adaptee adaptee;        public Adapter(Adaptee adaptee) {        this.adaptee = adaptee;    }public void adapteeMethod() {adaptee.adapteeMethod();}public void adapterMethod() {System.out.println("Adapter method!");    }}
    Client
    public class Test {    public static void main(String[] args) {        Target target = new Adapter(new Adaptee());        target.adapteeMethod();                target.adapterMethod();    }}
    Result
    Adaptee method!Adapter method!

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.