Design Mode-adapter Mode

Source: Internet
Author: User
Preface

Click to view: design mode series

Adapter Mode)

Definition: the adapter mode converts an interface of a class into another interface that the client expects, so that the two classes that the original interface does not match and cannot work together can work together.

Understanding: Multiple irrelevant classes match the same interface, and the client instantiates the interface to form a common class structure.

Example:

Online payment: (industrial and commercial payment and Construction payment) Obviously, this is the development done by two different developers. Therefore, the interface methods provided are not consistent and take it for granted.

Commonalities: Get the payment account, pay the arrears, and return the processing information.

Different: interfaces are provided with different method names;

Industrial and Commercial return processing information interface method name: gspay ()

CCB return processing information interface method name: jspay ()

In order to develop and use a public class to process these interfaces: sort out a common class for convenient development and calling and conciseness.

Class diagram:

Industrial and Commercial construction interface code:

// Industrial and Commercial Bank public class ICBC {// interface method provided by industrial and commercial bank public void gspay () {console. writeline ("I am an industrial and commercial bank, you want to use me! Saving money? ") ;}}// China Construction Bank public class CCB {// interface method provided by China Construction Bank Public void jspay () {console. writeline (" I Am a China Construction Bank, you want to use me! Get the money? ");}}

Adapter plug-in conversion code:

// Plug-in board public interface ipay {void pay () ;}// plug-in public class icbcapter: ICBC, ipay {public void pay () {base. gspay () ;}}// public class ccbapter: CCB, ipay {public void pay () {base. jspay ();}}

Client call:

Public static void main () {// Industrial and Commercial ipay = new icbcapter (); ipay. pay (); // construct ipay cpay = new ccbapter (); cpay. pay (); console. readline ();}
Summary adapter Mode

The adapter mode, like a socket, allows different types of electrical appliances to be switched over and run properly. Another example is transformer, which converts different voltages into uniform available voltages.

There is program restraint, and the conversion of an adapter is much simpler and more concise. Instead of cracking all the bank interfaces, you only need to study this adapter. Therefore, the adapter mode is still common and one of the easy-to-use modes.

Welcome to the Group at the top left of the blog for discussion, design mode, database, C #. net, and data structure.

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.