Adapter mode details, adapter details

Source: Internet
Author: User

Adapter mode details, adapter details
Definition of the adapter Mode

In computer programming, the adapter mode (sometimes called packaging style or packaging) adapts a class interface to what the user expects. An adaptation allows a class that cannot work together because the interface is not compatible, by encapsulating its own interface in an existing class.
There are two types of adapter modes:

Object Adapter Mode
* In this adapter mode, the adapter holds an instance of the class it wraps. In this case, the physical entity of the wrapped object is called properly.

Class adapter Mode
*-In this adapter mode, the adapter inherits its own classes.

Model:

Target: The Target role, which defines the conversion of other classes to the expected roles.

Adaptee: initial role, that is, our original class and interface object, such as "A-> B", where A is the initial role, which always exists

Adapter: the Adapter role, which is our focus today. The Adapter needs to be created, and the initial role needs to be converted to the target role through inheritance.

Code demonstration (class adapter mode ):

** 1. ** AbstractTarget: abstracts the target role

Public interface AbstractTarget {// abstract target role method public void target ();}

2.Specific target roles

Public class Target implements acttarget {@ Override public void target () {Log. d ("qzs", "implementation Target method ");}}

3.Initial role Adaptee:

Public class Adaptee {// initial method public void adaptee () {Log. d ("qzs", "Initial logic ");}}

4.Adapter role:

public class Adapter extends Adaptee implements AbstractTarget {    @Override    public void target() {       adaptee();    }}

5.Call

AbstractTarget tr=new Adapter();         tr.target();
Instance description

For example, I have an Android data cable and an iphone, and I want to use an Android data cable to charge my iphone. At this point:

Initial role: Android data cable

Target role: iphone

Adapter: data line Converter

Code Demonstration:

** 1. ** Android data cable:

Public class AndroidLine {public void charge () {Log. d ("qzs", "Android data cable charging Android phones ");}}

2.Target role:

public interface AbstractIphone {    public  void  target();} 

3.Data Line Converter

Public class PhoneLineAdapter extends AndroidLine implements extends actiphone {@ Override public void target () {super. charge (); Log. d ("qzs", "but I can use a data cable converter to charge my iphone ");}}

4.Call

AbstractIphone tr1=new PhoneLineAdapter();        tr1.target();

Run:

Advantages of the adapter mode and other

1. Advantages

Good Flexibility

The adapter allows two unrelated classes to run together.

Added the class permeability.

2. Note:

When there are multiple initial role interfaces and implementation classes, the write adapter cannot be inherited. You can use the constructor method in the adapter to transmit objects for data processing, that is, the Object Adapter mode.

 

Related Article

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.