Design pattern Learning (V) Adapter mode

Source: Internet
Author: User

Structural mode:

---core role: to achieve loose coupling from the structure of the program, so that the whole class structure can be enlarged to solve the larger problem

---categories:

Adapter mode, proxy mode, bridge mode, decoration mode, combo mode, appearance mode, enjoy meta mode

Adapter Mode: Principle:

--Convert a class interface into another interface that the customer wants.

Advantages:

Adapter mode makes it possible for classes to work together because they are incompatible with the interface

Roles in the pattern:
    • Destination interface (target): the interface that the customer expects. The target can be a specific or abstract class, or it can be an interface
    • Classes that need to be adapted (Adaptee): Classes or adapter classes that need to be adapted
    • Adapter (Adapter): By wrapping an object that needs to be adapted. Convert the original interface into a target interface
Implementation mode: 1: Class Adapter:
 Package Com.lp.adpater;  Public class extends Implements Target  {    @Override    publicvoid  handlereq () {        super. Request ();    }}
Class Adapter

2: Object Adapter:
 Package Com.lp.adpater;  Public class adaptee {    publicvoid  request () {        System.out.println ("ability to complete customer needs" );    }}
classes that need to be adapted
 Package Com.lp.adpater;  Public Interface Target {    void  handlereq ();}
Target Interface
 Package Com.lp.adpater;  Public class Implements Target  {    private  adaptee adaptee;    @Override    publicvoid  handlereq () {        adaptee.request ();    }      Public Adapter (adaptee adaptee) {        Super();         this. adaptee = adaptee;    }    }
Adapter
 PackageCom.lp.adpater;//Client Class//equivalent to a laptop computer with only USB interface Public classClient { Public voidtest1 (Target t) {t.handlereq (); }     Public Static voidMain (string[] arg) {Client C=NewClient (); Adaptee a=Newadaptee (); Target T=NewAdapter (a);    C.test1 (t); }    }
Client

Work in the scene:

-often used to retrofit and upgrade old systems

-If we don't need to maintain the system after development, many models are unnecessary, but unfortunately, the cost of maintaining a system is often several times that of developing a system.

The learning scenario is used in adapter mode:

--Java.io.InputStremReader (InputStream)

--Java.io.OutputStremReader (OutputStream)

Design pattern Learning (V) 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.