Java Adapter mode (adapter mode)

Source: Internet
Author: User

Adapter Mode definition: Jiuge two incompatible classes together, in a structured pattern, with two identities (adaptee) and adaptor (adapters).

Why use Adapter mode

We often encounter the need to combine two classes that are not related to each other, the first solution is to modify the interfaces of the respective classes, but if we do not have the source code, or we do not want to modify the respective interfaces for an application. What to do?

Using adapter, create a hybrid interface (half-breed) between the two interfaces.

How to use the adapter mode

Implement the adapter way, in fact "think in Java" "Class regeneration" section has been mentioned, there are two ways: combination (composition) and Inheritance (inheritance),

Suppose we want to pile, there are two kinds: square pile round pile.

 Public class squarepeg{  publicvoid  Insert (String str) {System.out.println ("Squarepeg insert ( ): "+str";}}  Public class roundpeg{  publicvoid  insertintohole (String msg) {System.out.println (" Roundpeg insertintohole (): "+msg";}    }

Now there is an application that requires both a square pile and a round pile to be played. Then we need to combine these two non-relational classes, assuming that roundpeg we do not have source code, or source code we do not want to modify, then we use adapter to implement this application:

 Public class 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, which is the appropriate ligand. The Pegadapter is adapter, adapting Adaptee (the adapter roundpeg) and Target (Squarepeg). In fact, this is the combination method (composition) and the Inheritance (inheritance) method is used synthetically.

Pegadapter first Inherits Squarepeg, then uses the combination of new to generate the object, generates Roundpeg object Roundpeg, and then overloads the parent class insert () method. From here, you also understand that using new to build objects differs from building objects using extends inheritance, which does not need to be modified for the original class, or even need to know its internal structure and source code.

If you have some experience with Java use, it has been found that this pattern is often used.

Further use

The above Pegadapter is inherited squarepeg, if we need to inherit from both sides, that is, inherit Squarepeg and inherit Roundpeg, because Java does not allow multiple inheritance, but we can implement (implements) two interfaces (interface ):

 Public Interface iroundpeg{  publicvoid  insertintohole (String msg);  Public Interface isquarepeg{  publicvoid  Insert (String str);}


The following is the new Roundpeg and Squarepeg, in addition to implementing the interface of the difference, and the above is no different.

 Public class Implements isquarepeg{  publicvoid  Insert (String str) {System.out.println ("squarepeg insert (): "+str);}}  Public class Implements iroundpeg{  publicvoid  insertintohole (String msg) {System.out.println (" Roundpeg insertintohole (): "+msg";}}

Here is the new pegadapter, called Two-way adapter:

 Public classPegadapterImplementsiroundpeg,isquarepeg{Privateroundpeg roundpeg;Privatesquarepeg squarepeg;//Construction Method    PublicPegadapter (Roundpeg peg) { This. roundpeg=peg;} //Construction Method    PublicPegadapter (Squarepeg Peg) ( This. squarepeg=peg;) Public voidInsert (String str) {roundpeg.insertintohole (str);}}

There is also a kind of called pluggable adapters, can dynamically get a few adapters in one. Using reflection technology, you can dynamically discover public methods in a class.

Java Adapter mode (adapter mode)

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.