Software Design Patterns and Architecture Experiments——3.2-1, 3.2-2 Application of Adapter Patterns

Link: Software Design Pattern and Architecture Experiment——2.1-1 (2) (Abstract) Application of Factory Pattern
Link: Software Design Pattern and Architecture Experiment——2.2-1 Application of Builder Pattern
Link: Software Design Pattern and System Structural Experiment——2.3-1 Application of Single Column Pattern
Link: Software Design Pattern and Architecture Experiment——3.1-1 Application of Combined Pattern

1. Adapter mode - 3.2-1 Customer information verification

1. Experimental purpose

  1. Master the characteristics of the adapter pattern
  2. Analyze specific problems and use the adapter pattern for design.

2. Experimental content

[Assignment 3.2-1] Regarding the function extension of the off-shelf product category CusInfo Validation used to verify customer information in Example 3.7. Requires the use of the Adapter pattern. For detailed requirements, see the Response Job section of the CD.

3. Schema UML Diagram

insert image description here
The design class diagram of the design program of this topic, and other diagrams:
insert image description here

4. Mode add code (JAVA language implementation)

CusInfoValidator class:

public abstract boolean isValidEmailAddr(String EmailAddr);

AdapterTestGUI class:

private JTextField txtCustomerName, txtAddress,
      txtZip,txtCellPhone, txtSSN,txtEmailAddr;
private JLabel lblCustomerName, lblAddress,
      lblZip, lblCellphone, lblSSN,lblEmailAddr;

txtEmailAddr=new JTextField(20);
lblEmailAddr= new JLabel("EmailAddr :");
UIPanel.add(lblEmailAddr);
UIPanel.add(txtEmailAddr);
gbc.gridx = 0;
gbc.gridy = 5;

gridbag.setConstraints(lblEmailAddr, gbc);
gbc.gridx = 1;
gbc.gridy = 5;
gridbag.setConstraints(txtEmailAddr, gbc);
gbc.gridx = 0;
gbc.gridy = 6;
public String getEmailAddr(){
    
    
   return txtEmailAddr.getText();
}
String emailaddr = getEmailAddr();
if(cusInfo.isValidEmailAddr(emailaddr)==false){
    
    
   dataTextArea.append("\nWrong format of EmailAddr.");
}
else{
    
    
   dataTextArea.append("\nCorrect format of EmailAddr.");
}

InformationAdapter class:

@Override
public boolean isValidEmailAddr(String EmailAddr) {
    
    
   boolean isValid=true;
   int a=0;
   int b=0;
   String ns = EmailAddr.trim();
   String nStr = ns.replaceAll("\\s{1,}","");
   int len = nStr.length();
   if ( (((nStr.charAt(0) >='A')&&(nStr.charAt(0)>='Z'))||
         ((nStr.charAt(0) >='a')&&(nStr.charAt(0) >='z'))) && (len>=5)) {
    
    
      for(int m=0; m<len; m++){
    
    
         if((Character.isLetter(nStr.charAt(m))==true)&&
               (Character.isDigit(nStr.charAt(m))==true)){
    
    
            isValid=false;
         }
         if(nStr.charAt(m)=='@'){
    
    
            a++;
         }
         if(nStr.charAt(m)>='0' && nStr.charAt(m)<='9'){
    
    
            b++;
         }
         if((m==0)&&(Character.isLetter(nStr.charAt(m))==false)){
    
    
            isValid=false;
         }
      }
      if(a!=1){
    
    
         isValid=false;
      }
      if(b==0){
    
    
         isValid=false;
      }
      return isValid;
   }
   else{
    
    
   return false;
}

5. Running results

insert image description here

6. Overall code

(1)AdapterTestGUI

package com.glut.xusheng;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class AdapterTestGUI extends JPanel{
    
    

	private JSplitPane splitPane;
	private JScrollPane btnPane,textPane;
	private JPanel UIPanel;
	private JTextArea dataTextArea;
	private JTextField txtCustomerName, txtAddress,
			txtZip,txtCellPhone, txtSSN,txtEmailAddr;
	private JLabel lblCustomerName, lblAddress,
			lblZip, lblCellphone, lblSSN,lblEmailAddr;
	public static final String VALIDATE = "Validate";
	public static final String EXIT = "Exit";

	public AdapterTestGUI(){
    
    
		super(new GridLayout(1,0));
		buildUpScrollGUI();
	}

	private void buildUpScrollGUI(){
    
    
		setUpButtonPanel();

		dataTextArea = new JTextArea(6, 10);
		btnPane = new JScrollPane(UIPanel);
		textPane = new JScrollPane(dataTextArea);

		splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		splitPane.setLeftComponent(btnPane);
		splitPane.setRightComponent(textPane );

		btnPane.setMinimumSize(new Dimension(500, 100));
		textPane.setMinimumSize(new Dimension(500, 200));
		splitPane.setDividerLocation(230);
		splitPane.setPreferredSize(new Dimension(500, 400));

		add(splitPane);
		setSize(new Dimension(500, 400));
	}

	private void setUpButtonPanel(){
    
    
		txtCustomerName = new JTextField(20);
		txtAddress = new JTextField(20);
		txtZip = new JTextField(20);
		txtCellPhone = new JTextField(20);
		txtSSN = new JTextField(20);
		txtEmailAddr=new JTextField(20);

		lblCustomerName = new JLabel("Customer Name:");
		lblAddress = new JLabel("Address:");
		lblZip = new JLabel("Zip Code:");
		lblCellphone = new JLabel("Cellphone Num:");
		lblSSN = new JLabel("SSN :");
		lblEmailAddr= new JLabel("EmailAddr :");

		//Create the open button
		JButton validateButton = new JButton(VALIDATE);
		validateButton.setMnemonic(KeyEvent.VK_V);
		JButton exitButton = new JButton(EXIT);
		exitButton.setMnemonic(KeyEvent.VK_X);
		ButtonListener objButtonHandler = new ButtonListener();

		validateButton.addActionListener(objButtonHandler);
		exitButton.addActionListener(objButtonHandler);

		UIPanel = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();
		UIPanel.setLayout(gridbag);
		GridBagConstraints gbc = new GridBagConstraints();

		UIPanel.add(lblCustomerName);
		UIPanel.add(txtCustomerName);
		UIPanel.add(lblAddress);
		UIPanel.add(txtAddress);
		UIPanel.add(lblZip);
		UIPanel.add(txtZip);
		UIPanel.add(lblCellphone);
		UIPanel.add(txtCellPhone);
		UIPanel.add(lblSSN);
		UIPanel.add(txtSSN);
		UIPanel.add(lblEmailAddr);
		UIPanel.add(txtEmailAddr);
		UIPanel.add(validateButton);
		UIPanel.add(exitButton);

		gbc.insets.top = 5;
		gbc.insets.bottom = 5;
		gbc.insets.left = 5;
		gbc.insets.right = 5;
		gbc.anchor = GridBagConstraints.WEST;

		gbc.gridx = 0;
		gbc.gridy = 0;
		gridbag.setConstraints(lblCustomerName, gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gridbag.setConstraints(txtCustomerName, gbc);
		gbc.gridx = 0;
		gbc.gridy = 1;
		gridbag.setConstraints(lblAddress, gbc);
		gbc.gridx = 1;
		gbc.gridy = 1;
		gridbag.setConstraints(txtAddress, gbc);
		gbc.gridx = 0;
		gbc.gridy = 2;
		gridbag.setConstraints(lblZip, gbc);
		gbc.gridx = 1;
		gbc.gridy = 2;
		gridbag.setConstraints(txtZip, gbc);
		gbc.gridx = 0;
		gbc.gridy = 3;
		gridbag.setConstraints(lblCellphone, gbc);
		gbc.gridx = 1;
		gbc.gridy = 3;
		gridbag.setConstraints(txtCellPhone, gbc);
		gbc.gridx = 0;
		gbc.gridy = 4;
		gridbag.setConstraints(lblSSN, gbc);
		gbc.gridx = 1;
		gbc.gridy = 4;
		gridbag.setConstraints(txtSSN, gbc);
		gbc.gridx = 0;
		gbc.gridy = 5;
		
		gridbag.setConstraints(lblEmailAddr, gbc);
		gbc.gridx = 1;
		gbc.gridy = 5;
		gridbag.setConstraints(txtEmailAddr, gbc);
		gbc.gridx = 0;
		gbc.gridy = 6;

		gbc.insets.left = 2;
		gbc.insets.right = 2;
		gbc.insets.top = 40;

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(validateButton);
		buttonPanel.add(exitButton);
		UIPanel.add(buttonPanel);
		gbc.gridx = 1;
		gbc.gridy = 6;
		gridbag.setConstraints(buttonPanel, gbc);
	}

	public String getName(){
    
    
		return txtCustomerName.getText();
	}
	public String getAddress(){
    
    
		return txtAddress.getText();
	}
	public String getZipCode(){
    
    
		return txtZip.getText();
	}
	public String getCellNum(){
    
    
		return txtCellPhone.getText();
	}
	public String getSSNNum(){
    
    
		return txtSSN.getText();
	}
	public String getEmailAddr(){
    
    
		return txtEmailAddr.getText();
	}

	class ButtonListener implements ActionListener{
    
    
		CusInfoValidator cusInfo = new InformationAdapter();
		public void actionPerformed(ActionEvent e){
    
    

			if (e.getActionCommand().equals(EXIT)){
    
    
				System.exit(1);
			}
			if (e.getActionCommand().equals(VALIDATE)){
    
    
				String name = getName();
				String address = getAddress();
				String zip = getZipCode();
				String cellNum = getCellNum();
				String ssn = getSSNNum();
				String emailaddr = getEmailAddr();

				if(cusInfo.isValidName(name)==false){
    
    
					dataTextArea.append("\nWrong format of name.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of name.");
				}

				if(cusInfo.isValidAddress(address)==false){
    
    
					dataTextArea.append("\nWrong format of address.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of address.");
				}

				if(cusInfo.isValidZipCode(zip)==false){
    
    
					dataTextArea.append("\nWrong format of zip code.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of zip code.");
				}


				if(cusInfo.isValidCellPhoneNum(cellNum)==false){
    
    
					dataTextArea.append("\nWrong format of cellphone number.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of cellphone number.");
				}

				if(cusInfo.isValidSSNNum(ssn)==false){
    
    
					dataTextArea.append("\nWrong format of SSN.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of SSN.");
				}
				if(cusInfo.isValidEmailAddr(emailaddr)==false){
    
    
					dataTextArea.append("\nWrong format of EmailAddr.");
				}
				else{
    
    
					dataTextArea.append("\nCorrect format of EmailAddr.");
				}
			}
		}
	} // End of class ButtonListener

	private static void createAndShowGUI() {
    
    
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("Adapter pattern demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		AdapterTestGUI newContentPane = new AdapterTestGUI();
		newContentPane.setOpaque(true);
		frame.setContentPane(newContentPane);
		frame.pack();
		frame.setVisible(true);
	}

	static public void main(String argv[]) {
    
    
		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    
    
			public void run(){
    
    
				createAndShowGUI();
			}
		});
	}
}

(2)CusInfoValidator

package com.glut.xusheng;
/*----------------------------------------------------*/
/* This interface is implemented by class Information */
/* Adapter, which inherits class InfoValidation.      */
/*----------------------------------------------------*/

public interface CusInfoValidator{
    
    
	public abstract boolean isValidName(String name);
	public abstract boolean isValidAddress(String address);
	public abstract boolean isValidZipCode(String zipCode);
	public abstract boolean isValidCellPhoneNum(String phoneNum);
	public abstract boolean isValidSSNNum(String SSNNum);

	public abstract boolean isValidEmailAddr(String EmailAddr);
}

(3)InformationAdapter

package com.glut.xusheng;
/*---------------------------------------------------------*/
/* This adapter class extends InfoValidation and           */
/* implements CusInfoValidator, and so the first           */
/* 4 functionalities listed in class CusInfoValidator      */
/* are automatically inherited from class InfoValidation,  */
/* and in this addapter class, isValidSSNNum(String SSNNum)*/
/* is emplemented.                                         */
/*---------------------------------------------------------*/


class InformationAdapter extends InfoValidation implements CusInfoValidator{
    
    

    /*------------------------------------------*/
	/* The Social Security number is a 9-digit  */
	/* number in the format "AAA-GG-SSSS".      */
	/*------------------------------------------*/
	public boolean isValidSSNNum(String SSNNum){
    
    
	   boolean isValid=true;
       String ns = SSNNum.trim();
	   String nStr = ns.replaceAll("\\s{1,}", "");
	   int len = nStr.length();

	   if ( (nStr.charAt(3) == '-') && (nStr.charAt(6) == '-') && (len==11) ) {
    
    
	      for(int m=0; m<len; m++){
    
    
		     if(  (m != 3) && (m !=6) && ( Character.isDigit(nStr.charAt(m))==false) ){
    
    
		        isValid=false;
		     }
	      }
	      return isValid;
	   }
	   else{
    
    
		  return false;
	   }
	}

	@Override
	public boolean isValidEmailAddr(String EmailAddr) {
    
    
		boolean isValid=true;
		int a=0;
		int b=0;
		String ns = EmailAddr.trim();
		String nStr = ns.replaceAll("\\s{1,}","");
		int len = nStr.length();
		if ( (((nStr.charAt(0) >='A')&&(nStr.charAt(0)>='Z'))||
				((nStr.charAt(0) >='a')&&(nStr.charAt(0) >='z'))) && (len>=5)) {
    
    
			for(int m=0; m<len; m++){
    
    
				if((Character.isLetter(nStr.charAt(m))==true)&&
						(Character.isDigit(nStr.charAt(m))==true)){
    
    
					isValid=false;
				}
				if(nStr.charAt(m)=='@'){
    
    
					a++;
				}
				if(nStr.charAt(m)>='0' && nStr.charAt(m)<='9'){
    
    
					b++;
				}
				if((m==0)&&(Character.isLetter(nStr.charAt(m))==false)){
    
    
					isValid=false;
				}
			}
			if(a!=1){
    
    
				isValid=false;
			}
			if(b==0){
    
    
				isValid=false;
			}
			return isValid;
		}
		else{
    
    
		return false;
	}
	}
}

(4)InfoValidation

package com.glut.xusheng;


class InfoValidation {
    
    


   /*----------------------------------------------------*/
   /* The name should be at least one character long     */
   /* and digital numbers should not appear in the names */
   /*----------------------------------------------------*/
   public boolean isValidName(String name){
    
    
	   boolean isValid=true;
	   String ns = name.trim();
	   String nStr = ns.replaceAll("\\b\\s{1,}\\b", "");
	   int len = nStr.length();

	   System.out.println("******Length = " + len);

	   if(len != 0 ){
    
    
	      for(int m=0; m<len; m++){
    
    
	         if(Character.isDigit(nStr.charAt(m))==true)
	            isValid=false;
	      }
	      return isValid;
       }
       else{
    
    
	      return false;
	   }
   }
   /*----------------------------------------------------*/
   /* The address should be at least 10 character long   */
   /*----------------------------------------------------*/
   public boolean isValidAddress(String address){
    
    

	   char[] ca = address.trim().toCharArray();
	   int aLen = ca.length;

       if ( aLen < 10 ){
    
    
          return false;
	   }
       else{
    
    
	      return true;
	   }
   }
   /*-------------------------------------------------------*/
   /* The zip code should contain exactly 9 digital integer */
   /* numbers. Only digital numbers are allowed in the zip  */
   /* code. Spaces are allowed in the zip code              */
   /*-------------------------------------------------------*/
   public boolean isValidZipCode(String zipCode){
    
    

       boolean isValid=true;
	   String ns = zipCode.trim();
	   String nStr = ns.replaceAll("\\b\\s{1,}\\b", "");
	   int len = nStr.length();

	  if (len == 9){
    
    
	     for(int n=0; n<len; n++){
    
    
		    if(Character.isDigit(nStr.charAt(n))==false){
    
    
	           isValid = false;
	        }
         }
         return isValid;
	  }
      else{
    
    
		 System.out.println("Length is not 9");
	     return false;
	   }
   }

   /*--------------------------------------------------------*/
   /* The cellPhone number should contain exactly 11 digital */
   /* integer numbers. Only digital numbers are allowed in   */
   /* the zip code.Spaces are allowed in the zip code        */
   /*--------------------------------------------------------*/
   public boolean isValidCellPhoneNum(String phoneNum){
    
    

       boolean isValid=true;
	   String ns = phoneNum.trim();

	   String nStr = ns.replaceAll("\\b\\s{1,}\\b", "");
	   int len = nStr.length();

   	  if (len == 11 ){
    
    
   	     for(int n=0; n<len; n++){
    
    
			if(Character.isDigit(nStr.charAt(n))==false){
    
    
   	            isValid = false;
   	        }
		 }
   	     return isValid;
      }
      else{
    
    
		  System.out.println("Length is not 11");
   	      return false;
   	  }
   }


}// end of class

7. Experiment summary

Adapter pattern
Adapter pattern (Adapter Pattern): Convert an interface into another interface that the customer wants, so that those classes that are not compatible with the interface can work together, and its alias is the wrapper (Wrapper). The Adapter pattern can be used both as a class-structural pattern and as an object-structural pattern.
In the adapter mode, we solve the problem of interface incompatibility by adding a new adapter class, so that classes that have no relationship can work together.
According to the relationship between the adapter class and the adapter class, the adapter pattern can be divided into object adapter and class adapter. In the object adapter pattern, there is an association relationship between the adapter and the adapter; in the class adapter pattern, the adapter and There is an inheritance (or implementation) relationship between adapters.
Role

Target (target abstract class): The target abstract class defines the interface required by the customer, which can be an abstract class or interface, or a concrete class.
Adapter (adapter class): The adapter can call another interface as a converter to adapt Adaptee and Target. The adapter class is the core of the adapter pattern. In the object adapter, it inherits Target and associates an Adaptee are connected.
Adaptee (adapter class): The adapter is the role to be adapted. It defines an existing interface that needs to be adapted. The adapter class is generally a concrete class that includes the business that the customer wants to use method, and in some cases there may be no source code for the adapter class.

2. Adapter Mode - 3.2-2 Zip Code Inspection

1. Experimental purpose

  1. Master the characteristics of the adapter pattern
  2. Analyze specific problems and use the adapter pattern for design.

2. Experimental content

[Homework 3.2-2] About the function expansion of the postal code inspection system. It is required to use the object adapter mode. For the specific problem description and design class diagram, please refer to the additional column 3.1 in section 3.2 of the CD and the description document of the corresponding job.

3. Schema UML Diagram

insert image description here
The design class diagram of the design program of this topic, and other diagrams:
insert image description here
insert image description here
insert image description here

4. Mode add code (JAVA language implementation)

Question: Understand the following class diagram. Except for the codes of classes ChinesePostalCode and ChinesePostalCodeAdapter, all the source codes for using the Java Object Adapter pattern are given to you.
(1) Write the code of the class ChinesePostalCode, where the code for writing the method is validichinesepcode(pcode:String). The correct format of China's postal code has a format similar to 150001, that is, 6 digits.

package com.glut.xusheng;

public class ChinesePostalCode {
    
    
    private boolean digitFlag = true;
    private boolean characterFlag = true;


    /*=======================================================*/
    /* Canadian postal code has the form CDC DCD, where      */
    /* C is an English character and D is any digit from     */
    /* 0 to 9. A space is required after the 3rd character,  */
    /* but this is not checked in this program. And any      */
    /* number of spaces is allowed in the user input postal  */
    /* code.                                                 */
    /*=======================================================*/
    public boolean isValidZipCode(String postalCode, String province) {
    
    

        char[] pCode = toNonBlankCharArray(postalCode);

        if (pCode.length != 6)
            return false;

        for(int i=0; i<pCode.length/2; i++){
    
    
            if( Character.isLetter(pCode[2*i]) == false)
                characterFlag = false;
            if(Character.isDigit(pCode[2*i+1]) == false)
                digitFlag = false;
        }

        if ( (digitFlag == false) || (characterFlag==false))
            return false;
        else
            return true;
    }

    /*====================================================*/
    /* Get rid of all the spaces from the user input      */
    /*====================================================*/
    public char[] toNonBlankCharArray(String postalCode){
    
    
        int m=0;

        for (int k=0;k<postalCode.length(); k++){
    
    
            if (Character.isSpaceChar(postalCode.charAt(k)) == false ){
    
    
                m++;
            }
        }

        char[] p = new char[m];

        int n = 0;
        for (int k=0;k<postalCode.length(); k++){
    
    
            if (Character.isSpaceChar(postalCode.charAt(k)) == false ){
    
    
                p[n] =  postalCode.charAt(k);
                n++;
            }
        }
        return p;
    }


}

(2) Then write code for the class ChinesePostalCodeAdapter.

package com.glut.xusheng;


public class ChinesePostalCodeAdapter extends ChinesePostalCode implements ZipCodeValidator  {
    
    

        private ChinesePostalCode objCPostCode;

        public ChinesePostalCodeAdapter(ChinesePostalCode pCode) {
    
    
            objCPostCode = pCode;
        }
        public boolean isValidZipCode(String zip, String state) {
    
    
            return isValidZipCode(zip, state);
        }
    }// end of class

(3) If needed, add some code in the ZipCodeTester class to make the program work for all US ZIP codes, Canadian ZIP codes and Chinese ZIP codes.
Customer class:

if (country.equals(Customer.CHINA)) {
    
    
   ChinesePostalCode chinesePostalCode = new ChinesePostalCode();

   return chinesePostalCode.isValidZipCode(zip, state);
}

5. Running results

insert image description here

insert image description here

6. Experiment summary

Adapter mode summary
1. Main advantages:

(1) Decouple the target class and the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original structure.
(2) Increase the transparency and reusability of the class, and encapsulate the specific business implementation process in the adapter class, which is transparent to the client class, and improves the reusability of the adapter. An adapter class can be reused in many different systems.
(3) The flexibility and scalability are very good. By using the configuration file, the adapter can be easily replaced, and a new adapter class can be added without modifying the original code, which fully complies with the "open and close principle".

Specifically, the class adapter pattern has the following advantages: Since the adapter class is a subclass of the adapter class, some methods of the adapter can be replaced in the adapter class, making the adapter more flexible.
The object adapter pattern also has the following advantages: An object adapter can adapt multiple different adapters to the same target;
it can adapt a subclass of an adapter, because the adapter and the adapter are related, According to the "Liskov substitution principle", subclasses of the adapter can also be adapted through the adapter.
The disadvantages of the class adapter pattern are as follows: For languages ​​that do not support multiple class inheritance, such as Java and C#, at most one adapter class can be adapted at a time, and multiple adapters cannot be adapted at the same time; the adapter class cannot be the final
class , For example, it cannot be a final class in Java, and it cannot be a sealed class in C#;
in Java, C# and other languages, the target abstract class in the class adapter pattern can only be an interface, not a class, and its use has certain limitations.

The disadvantages of the object adapter pattern are as follows:

Compared to the class adapter pattern, it is cumbersome to replace some methods of the adapter class in the adapter. If you must replace one or more methods of the adapter class, you can first make a subclass of the adapter class, replace the methods of the adapter class, and then treat the subclass of the adapter class as a real Adapters to adapt, the implementation process is more complicated.

2. Applicable scenarios:
The system needs to use some existing classes, and the interfaces (such as method names) of these classes do not meet the needs of the system, and there is even no source code of these classes.
Want to create a class that can be reused to work with some classes that are not very related to each other, including some classes that may be introduced in the future.

3. Pattern structure and implementation
The adapter pattern can be realized by multiple inheritance. For example, C++ can define an adapter class to simultaneously inherit the business interface of the current system and the existing component interface in the existing component library; Java does not support multiple inheritance. But you can define an adapter class to realize the business interface of the current system, and at the same time inherit the existing components in the existing component library.

The object adapter pattern can be used to introduce the components that have been implemented in the existing component library into the adapter class, and this class realizes the business interface of the current system at the same time. Now to introduce their basic structure.

  1. Structure of the pattern
    Adapter pattern (Adapter) contains the following main roles.
  2. Target (Target) interface: the interface expected by the current system business, which can be an abstract class or an interface.
  3. Adaptee class: It is the component interface in the existing component library to be accessed and adapted.
  4. Adapter (Adapter) class: It is a converter that converts the adapter interface into the target interface by inheriting or referencing the adapter object, allowing customers to access the adapter in the format of the target interface.

The structure diagram of the class adapter pattern is shown in the figure:

The structure diagram of the object adapter pattern is shown in the figure:
insert image description here

Guess you like

Origin blog.csdn.net/m0_52435951/article/details/125071398