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