Adapter mode -- Design Mode

1. overview

Convert the interface of one class to another that the customer wants. The Adapter pattern allows classes that could not work together because of incompatible interfaces to work together.

2. Roles in patterns

  • Target: the interface expected by the customer. The target can be a concrete or abstract class, or an interface.
  • Adaptee: the class or adapter class that needs to be adapted.
  • Adapter: transform the original interface into the target interface by wrapping an object that needs to be adapted.

3. Implementation mode

  • Adapter pattern of class (implemented by inheritance)
  • Object adapter (implemented by object composition)

4. code

// Existing classes with special functions but not conforming to our existing standard interfaces
class Adaptee {
	public void specificRequest() {
		System.out.println("Adaptees have special functions...");
	}
}
 
// Target interface, or standard interface
interface Target {
	public void request();
}
 
// Specific target class, only providing common functions
class ConcreteTarget implements Target {
	public void request() {
		System.out.println("Common class has common functions...");
	}
}
 
// Adapter class, inheriting the adapted class and implementing the standard interface
class Adapter extends Adaptee implements Target{
	public void request() {
		super.specificRequest();
	}
}
 
// Test class public class Client{
	public static void main(String[] args) {
		// Use common function class
		Target concreteTarget = new ConcreteTarget();
		concreteTarget.request();
		
		// Use special function class, i.e. adaptation class
		Target adapter = new Adapter();
		adapter.request();
	}
}

Test results:

Common classes have common functions
 Adaptive class has special functions

The Adapter implemented above is called a class Adapter, because the Adapter class inherits both the Adapter (adapted class) and the Target interface (Java does not support multiple inheritance, so we can implement it). In the Client class, we can select and create any subclass that meets the needs to implement specific functions. Another Adapter pattern is the object Adapter, which is not implemented by multiple inheritance or inheritance, but by direct association or delegation.

The code is as follows:

// Adapter class, directly related to the adapted class, while implementing the standard interface
class Adapter implements Target{
	// Directly related adaptive class
	private Adaptee adaptee;
	
	// You can pass in the adapted class object to be adapted through the constructor
	public Adapter (Adaptee adaptee) {
		this.adaptee = adaptee;
	}
	
	public void request() {
		// Here is the use of delegation to complete special functions
		this.adaptee.specificRequest();
	}
}
 
// Test class
public class Client {
	public static void main(String[] args) {
		// Use common function class
		Target concreteTarget = new ConcreteTarget();
		concreteTarget.request();
		
		// Using special function classes, i.e. adaptation classes,
		// You need to create an object of the adapted class as a parameter first
		Target adapter = new Adapter(new Adaptee());
		adapter.request();
	}
}

The test results are consistent with the above. From the class diagram, we also know that only the internal structure of the Adapter class needs to be modified, that is, the Adapter itself must first have an object of the adapted class, and then delegate specific special functions to this object for implementation. Using the object Adapter pattern, the Adapter class (Adapter class) can achieve the function of adapting multiple different adapted classes according to the incoming Adapter object. Of course, at this time, we can extract an interface or abstract class for multiple adapted classes. It looks like the object Adapter pattern is a little more flexible.

5. Mode summary

Advantage

  • Through the adapter, the client can call the same interface, so it is transparent to the client. This is simpler, more direct and more compact.
  • Reusing the existing classes solves the problem of inconsistency between the existing classes and the reuse environment.
  • Decouple the target class and the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original code.
  • An object adapter can adapt multiple different adapter classes to the same target, that is, the same adapter can adapt both the adapter class and its subclasses to the target interface.

shortcoming

  • For the object adapter, the implementation process of replacing the adapter is complex.

Applicable scenario

  • The system needs to use existing classes whose interfaces do not match those of the system.
  • We want to build a reusable class to work with some classes that are not very related to each other, including some that may be introduced in the future.
  • When two classes do the same or similar things, but have different interfaces.
  • The old system developed classes have implemented some functions, but the client can only access in the form of another interface, but we do not want to manually change the original classes.
  • Using the third-party component, the component interface definition is different from its own definition. You do not want to modify your own interface, but you need to use the functions of the third-party component interface.
Published 13 original articles, won praise 0, visited 169
Private letter follow

Keywords: Java

Added by php2MySQL on Tue, 21 Jan 2020 06:44:58 +0200