Adapter mode in Design Mode)

Source: Internet
Author: User

1. Definition

The interface of a class is transformed into another interface expected by the client, so that the two classes that cannot work together due to interface mismatch can work together. The adapter mode is also called the transformer mode, or wrapper mode.

2. generic class diagram

Target role: this role defines the interface for converting other classes, that is, our expected interface.

Adaptee source role: who you want to convert to the target role? The "who" is the original role. It is an existing and running class or object packaged by the adapter role, it will become a brand new and beautiful role.

Adapter adapter role: the core role in the adapter mode. The other two roles are existing roles, and the adapter role needs to be created. This role has simple responsibilities, the source role is converted to the target role by means of integration or class association.

3. Code List

Package adapter;/*** target role * 2:13:53-limiracle */public interface target {// The target role has its own method public void request ();}
Package adapter;/*** implementation class of the target role * 2:15:21-limiracle */public class concretetarget implements target {public void request () {system. out. println ("= ");}}
Package adapter;/*** source role * 2:16:04-limiracle */public class adaptee {// original business logic public void dosomething () {system. out. println ("********");}}
Package adapter;/*** adapter role * 2:17:23-limiracle */public class adapter extends adaptee implements target {// target implementation interface, it also calls what the source role has to do. Public void request () {super. dosomething ();}}
Package adapter; public class main {public static void main (string [] ARGs) {// original business logic Target target = new concretetarget (); target. request (); // the business logic target TARGET2 = new adapter (); target2.request () ;}} after the adapter role is added ();}}

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.