The adaptor of design pattern learning

Source: Internet
Author: User

Adapter of design mode ( adapter )

Adapter Mode definition:
The use of two incompatible classes jiuge in a structured pattern requires adaptee (the adapter) and
Adaptor (adapter) two identities.
Why use?
We often encounter the need to combine two classes that are not related to each other, the first solution is to modify the access
Port, but if we do not have source code, or we do not want to modify the respective interface for an application. How
Do?
Using Adapter, create a hybrid interface (half-breed) between the two interfaces.
How to use?
Implementing the Adapter way, in fact, "think in Java," The "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{
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 a square pile and a round pile. So we need to combine these two non-related classes
Application. Suppose roundpeg we have no source code, or source code we do not want to modify, then we use 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, which is the appropriate ligand. Pegadapter is Adapter, will
Adaptee (Roundpeg by the adapter) and target (destination squarepeg). This is actually the group
Combined method (composition) and inheritance (inheritance) methods.
Pegadapter first inherits Squarepeg and then uses the combination of new to generate the object, generating the Roundpeg
Object Roundpeg, and then overloads the parent class insert () method. From here, you also learn to use new to generate objects and make
It is not necessary to modify the original class or even to know the internal structure of the generated object with extends inheritance.
and source code.
If you have some experience with Java use, it has been found that this pattern is often used.
Further use
Above the Pegadapter is inherited squarepeg, if we need to inherit from both sides, that is, inherit Squarepeg
Also inherit Roundpeg, because Java does not allow multiple inheritance, but we can implement (implements) two
Port (interface)
public interface iroundpeg{
public void Insertintohole (String msg);
}
public interface isquarepeg{
public void 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 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);
}
}
Here is the new pegadapter, called Two-way adapter:
public class Pegadapter implements iroundpeg,isquarepeg{
Private Roundpeg roundpeg;
Private Squarepeg squarepeg;
Construction method
Public Pegadapter (Roundpeg peg) {this.roundpeg=peg;}
Construction method
Public Pegadapter (Squarepeg peg) (this.squarepeg=peg;)
public void Insert (String str) {roundpeg.insertintohole (str);}
}
There is also a kind of called pluggable adapters, can dynamically get a few adapters in one. Use
Reflection technology, you can dynamically discover the public method in the class.

The adaptor of design pattern learning

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.