[Design mode-structural] Adapter)

Source: Internet
Author: User
Summary

Name

Adapter

Structure

Motivation

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

  • You want to use an existing class, and its interface does not meet your needs.

  • You want to create a reusable class that can work together with other unrelated classes or unforeseen classes (that is, classes whose interfaces may not be compatible.

  • (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.

Analysis

Image analogy:

Adapter-I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, and she can't speak Mandarin, so I have to turn to my friend Kent, as an adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)

Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can return a suitable instance to the client based on the parameters.

1. Target
Define the interfaces used by the client in specific fields.

2. Client
Works with objects that comply with the target interface.

3. adaptee
Define an existing interface, which must be adapted.

4. Adapter
Adapt the adaptee interface to the target interface.

In a word, the method of an instance of another class can be called in an instance of another class.

Instance

The files involved include:

Target. Java-called Interface

Adaptee. Java-adapted class

Adapter. Java-adaptive class (the instance of the adapted class acts as an attribute of this instance)

Testmain. Java

/**    * @author oscar999    * @date 2013-7-23 * @version V1.0    */package designptn.adapter;public interface Target {void adapteeMethod();void adapterMethod();}

/**    * @author oscar999    * @date 2013-7-23 * @version V1.0    */package designptn.adapter;public class Adaptee {public void adapteeMethod() {System.out.println("Adaptee Method!");}}
/**    * @author oscar999    * @date 2013-7-23 * @version V1.0    */package designptn.adapter;public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee){this.adaptee = adaptee;}@Overridepublic void adapteeMethod() {// TODO Auto-generated method stubadaptee.adapteeMethod();}@Overridepublic void adapterMethod() {// TODO Auto-generated method stubSystem.out.println("Adapter Method!");}}

/**    * @author oscar999    * @date 2013-7-23 * @version V1.0    */package designptn.adapter;public class TestMain {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubTarget target = new Adapter(new Adaptee());target.adapteeMethod();target.adapterMethod();}}


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.