Java design pattern type adapter pattern

Keywords: socket Java

Article directory

1: Adapter mode

Basic introduction

  • The adapter pattern transforms the interface of a class into another interface representation expected by the client. The main purpose is compatibility, so that two classes that could not work together due to interface mismatch can work together. Its alias is wrapper
  • Adapter mode belongs to structural mode
  • There are three types: class adapter mode, object adapter mode and interface adapter mode

Working principle

  • Adapter mode: convert the interface of one class to another. Make the classes that are not compatible with the original interface compatible
  • From the user's point of view, there is no adapter, which is decoupled
  • The user calls the target interface method transformed by the adapter, and the adapter calls the relevant interface method of the adapter
  • When the user receives the feedback result, he only feels that he interacts with the target interface, as shown in the figure below

    Thailand's socket (adapter) and China's socket (target) are different, you can buy a multi-functional adapter (adapter) to achieve

1.1 adapter mode

Basic introduction

  • Adapter class, through inheriting src class, implements dst class interface, completes src - > dst adaptation

Class adapter pattern application instance

1) Take the example of charger in life to explain the adapter. The charger itself is equivalent to Adapter,220V AC is equivalent to SRE, and our target DST is 5V DC
2) Thought analysis:

3) Code demonstration
Voltage220V .class

//Class src
public class Voltage220V {
	
	public int output220V() {
		
		int src=220;
		System.out.println("output voltage"+src+"V");
		return src;
		
	}
}

IVoltage5V .interface

//dst interface
public interface IVoltage5V {
	
	public int output5V();
}

VoltageAdapter .class

//Class Adapter
//Because it inherits the src class, it can rewrite the methods of the src class according to the requirements, which makes the Adapter more flexible
public class VoltageAdapter extends Voltage220V implements IVoltage5V{	
	public int output5V() {
		
		int srcV = output220V();
		int dstV = srcV / 44 ; //Turn to 5v
		return dstV;
		
	}
}

Phone .class

public class Phone {
	//Charge
		public void charging(IVoltage5V iVoltage5V) {
			if(iVoltage5V.output5V() == 5) {
				System.out.println("Voltage 5 V, Can charge~~");
			} else if (iVoltage5V.output5V() > 5) {
				System.out.println("Voltage greater than 5 V, Can not charge~~");
			}
		}

}

Client .class

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(" === Class adapter pattern ====");

		IVoltage5V iVoltage5V=new VoltageAdapter();
		new Phone().charging(iVoltage5V);
	}

}

1.1.1 precautions and details of type 1 adapter mode

  • Java is a single inheritance mechanism, so class adapter needs to inherit src class, which is a disadvantage, because it requires that dst must be an interface, which has some limitations;
  • The methods of src class will be exposed in the Adapter, which also increases the cost of use.
  • Because it inherits the src class, it can rewrite the methods of src class according to the requirements, which makes the Adapter more flexible.

1.2 object adapter mode

Basic introduction

  • The basic idea is the same as the Adapter pattern of the class. It only modifies the Adapter class, not inherits the src class, but holds the instance of the src class to solve the compatibility problem. Namely: hold the sre class, implement the dst class interface, and complete the src - > dst adaptation
  • According to the principle of composite reuse, we try to use Association (aggregation) instead of inheritance in the system.
  • The object adapter pattern is one of the commonly used adapter patterns

Application instance of object adapter pattern
1) Take the example of charger in life to explain the adapter. The charger itself is equivalent to Adapter,220V AC is equivalent to SRE (i.e. adapter). Our target DST (i.e. target) is 5V DC, which is completed by using the object adapter mode.
2) Thought analysis (class diagram): just modify the adapter,
As follows:

3) Code implementation
Voltage220V .class

//Class src
public class Voltage220V {
	
	public int output220V() {
		
		int src=220;
		System.out.println("output voltage"+src+"V");
		return src;
		
	}
}

IVoltage5V .interface

//dst interface
public interface IVoltage5V {
	
	public int output5V();
}

VoltageAdapter .class

//Class Adapter
//Because it inherits the src class, it can rewrite the methods of the src class according to the requirements, which makes the Adapter more flexible
public class VoltageAdapter extends Voltage220V implements IVoltage5V{	

	private Voltage220V voltage220v;
	
	public VoltageAdapter(Voltage220V voltage220v) {
		this.voltage220v=voltage220v;
	}
	public int output5V( ) {
		
		int srcV = voltage220v.output220V();
		int dstV = srcV / 44 ; //Turn to 5v
		return dstV;
		
	}

}

Phone .class

public class Phone {
	//Charge
		public void charging(IVoltage5V iVoltage5V) {
			if(iVoltage5V.output5V() == 5) {
				System.out.println("Voltage 5 V, Can charge~~");
			} else if (iVoltage5V.output5V() > 5) {
				System.out.println("Voltage greater than 5 V, Can not charge~~");
			}
		}

}

Client .class

public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(" === Object Adapter Pattern  ====");

		new Phone().charging(new VoltageAdapter(new Voltage220V()));
	}

}

1.2.1 precautions and details of object adapter mode

  • Object adapter and class adapter are the same idea, but they are implemented in different ways. According to the principle of composite reuse, it uses combination instead of inheritance, so it solves the limitation that class adapter must inherit src, and it no longer requires dst to be interface.
  • Lower cost and more flexibility.

1.3 interface adapter mode (default adapter mode)

Basic introduction

  • Some books are called: default adapter pttern or default adapter mode.
  • Core idea: when not all the methods provided by the interface need to be implemented, an abstract class can be designed to implement the interface first, and a default implementation (empty method) can be provided for each method in the interface. Then the subclass of the abstract class can selectively cover some methods of the parent class to implement the requirements
  • This applies to situations where an interface does not want to use all its methods.

Code example
interface1 .class

public interface interface1 {

	public void demo1();
	public void demo2();
	public void demo3();
	public void demo4();

}

AbstractAdapter .class

/*When you do not need to implement all the methods provided by the interface,
You can design an abstract class to implement the interface, and provide a default implementation (empty method) for each method in the interface*/
public abstract class AbstractAdapter implements interface1{
	
	public void demo1() {};
	public void demo2() {};
	public void demo3() {};
	public void demo4() {};
}

Client .class

package com.atstudying.interfaceadapter;

public class Client {

	public static void main(String args[]) {
		
		AbstractAdapter abstractAdapter=new AbstractAdapter() {
			
			public void demo1() {
				System.out.println("demo1");
				
			}
			//Subclass of abstract class can selectively cover some methods of parent class to implement requirements
		};
		
		abstractAdapter.demo1();
	}
}

The running result shows that when not all the methods provided by the interface need to be implemented, an abstract class can be designed to implement the interface first, and a default implementation (empty method) can be provided for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements

2: Adapter mode considerations and details

  • The three naming methods are based on how src is given to the Adapter (the form in the Adapter).
  • Class Adapter: give to by class. In Adapter, src is regarded as a class. Inherit object Adapter: give to by object. In Adapter, src is regarded as an object. Hold interface Adapter: give to by interface. In Adapter, src is regarded as an interface
  • The most important function of Adapter mode is to integrate the incompatible interfaces.
  • In the actual development, the implementation is not limited to the three classic forms we explain
151 original articles published, praised 69, visitors 2972
Private letter follow

Posted by the mysox1 on Wed, 15 Jan 2020 01:02:50 -0800