Design Pattern [06] Adapter pattern

define

Translate the interface of a class into another interface that the customer wants.

What is the main solution?

The main solution is in the software system, often to put some “existing object” into the new environment, and the new environment requirements of the interface is the object can not meet.

structure

  1. Target interface: The interface expected by the current system service. It can be an abstract class or interface.
  2. Adaptee class: It is a component interface in an existing component library that is accessed and adapted.
  3. Adapter class: It is a converter that converts the Adapter interface into the target interface by inheriting or referencing the Adapter’s object, allowing customers to access the Adapter in the format of the target interface.

Structure diagram of the class adapter pattern

Structure diagram of the object adapter pattern

Class adapter pattern implementation

package adapter;
// Target interface
interface Target
{
    public void request(a);
}
// Adaptor interface
class Adaptee
{
    public void specificRequest(a)
    {       
        System.out.println("The business code in the adapter is called!"); }}// Class adapter class
class ClassAdapter extends Adaptee implements Target
{
    public void request(a)
    { specificRequest(); }}// Client code
public class ClassAdapterTest
{
    public static void main(String[] args)
    {
        System.out.println("Class adapter pattern test:");
        Target target = newClassAdapter(); target.request(); }}// The program runs as follows:Class adapter pattern test: the business code in the adapter is called!Copy the code

Object adapter pattern implementation

The code for the “target interface” and “adapter classes” in the object adapter pattern is the same as the code for the adapter class and the client.

package adapter;
// Object adapter class
class ObjectAdapter implements Target
{
    private Adaptee adaptee;
    public ObjectAdapter(Adaptee adaptee)
    {
        this.adaptee=adaptee;
    }
    public void request(a)
    { adaptee.specificRequest(); }}// Client code
public class ObjectAdapterTest
{
    public static void main(String[] args)
    {
        System.out.println("Object adapter pattern test:");
        Adaptee adaptee = new Adaptee();
        Target target = newObjectAdapter(adaptee); target.request(); }}// The program runs as follows:Object adapter pattern test: business code in adapter is invoked!Copy the code

Apps on Android

Different data providers use an adapter to provide services to the same customer. An Adapter for the ListView or GridView.

conclusion

Advantages:

You can have any two unrelated classes run together.

2. Improved class reuse.

3. Increased class transparency.

4. Good flexibility.

Disadvantages:

1. Too much use of adapters will make the system very messy and difficult to grasp as a whole. For example, clearly see is called A interface, in fact, internal adaptation into the IMPLEMENTATION of THE B interface, A system if too much of this situation, is tantamount to A disaster. So if you don’t have to, you can skip the adapter and refactor your system directly.

2. Since JAVA inherits at most one class, at most one adaptor class can be adapted, and the target class must be abstract.

** Usage scenarios: ** The adapter pattern should be considered when you are motivated to modify the interface of a functioning system.

** Adapters are not added at detailed design time, but rather to solve problems of projects that are in service.

The resources

JAVA Design Patterns summary of 23 design patterns

Thank you for reading this article. I hope you can use it on our wechat official account (see the QR code below),The Denver nuggets,Jane’s book,CSDNCommunicate together.

CoderWonder_GZH.jpg