Java design mode--Adapter mode

Source: Internet
Author: User

Adapter mode (Adapter): Converts the interface of a class into the interface that the client wants

1. Being adapted to the object

2. Adapter

3. Target interface

is to connect the object and the target interface, all dealing with the adapter, the adaptation object and the target interface is not directly interactive, to achieve loose coupling, the adapter is like a USB and a circular port two ways of connecting cable.

The computer requires a USB keyboard, but the keyboard is round-mouth.

Notebook PC

//Computer Public classDiannao { Public voidShanchu (Target t) {//dealing with adapter interfaces rather than keyboardsT.chuli (); }     Public Static voidMain (string[] args) {Diannao dn=NewDiannao (); Target T=NewAdapter (NewJianpan ());            Dn.shanchu (t); }    }

Keyboard

// Keyboard is adapted to the object  Public class Jianpan {        publicvoid  Dazi () {        System.out.println ("press DELETE");}    }

Adapter interface, and a layer of abstraction

 Public Interface Target {        void  Chuli ();}

Implementation classes for adapters

//Adapter Public classAdapterImplementstarget{//and be associated with the adapted object.//1 Inheriting the keyboard class adapter mode//2 Combo Keyboard object (recommended) object Adapter mode        PrivateJianpan Jianpan;  Public voidChuli () {Jianpan.dazi (); }     PublicAdapter (Jianpan jianpan) { This. Jianpan =Jianpan; }}

It is recommended to use the above object to fit

As an example, the flow wrapper class is an adaptation process.

Java design 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.