Adapter in Design Mode)

Source: Internet
Author: User

Adapter mode definition:
To use two incompatible classes together, it is a structural mode and requires two identities: Adaptee and Adaptor.

Why?
We often encounter the need to combine two unrelated classes. 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 it?
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 apply these two unrelated classes comprehensively. if we do not have the source code or do not want to modify the source code of RoundPeg, 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 code above, RoundPeg belongs to Adaptee and is the 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.

Further use
The above PegAdapter inherits 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 two interfaces (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 new PegAdapter is 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.


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.