Home  >  Article  >  Java  >  Example sharing about adapter pattern in Java

Example sharing about adapter pattern in Java

黄舟
黄舟Original
2017-09-23 10:04:011602browse

This article mainly introduces relevant information about the adapter pattern of the design pattern in Java. The adapter pattern converts the interface of a class into another interface expected by the customer. Adapters allow originally incompatible classes to work together closely. Friends in need can refer to the adapter pattern of design patterns in java

Preface:

The adapter pattern can apply a class or interface to another different but related interface. The main method is to declare an implementation class of the target interface and declare an implementation class in the class that will be adapted. The constructor of the matching class or interface (adapter) and the instance of the adaptee are used as parameters, so that when the target interface is implemented, the instance of the adaptee can be called, supplemented by some additional operations. The main body of the adapter pattern has three parts: the adapter, the adapter implementation class and the adaptee. The specific class structure is as follows:

Here, the objects of the adaptee are put into the adapter class in a combined manner, then both the adaptee and its implementer can Use this adapter. The advantage of the adapter pattern is that it can make objects of different types but with similar working purposes compatible. The disadvantage is that if the target interface is large, a lot of extra code is required to perform the compatibility work.

Here is an example that is not very realistic to illustrate the adapter pattern. Both turkeys and ducks can quack and fly, but turkeys and ducks quack differently, and turkeys can't fly as far as ducks. If you want to disguise a turkey as a duck, you need to dress the turkey object in some ways. Here we can declare a dress class, which is also a type of duck. When the turkey enters the dress, it will Turned into a duck. The specific class implementation is as follows:

Target interface (duck interface):

public interface Duck {
  void quack();
  void fly();
}

Normal implementation of the target interface (duck implementation):

public class MallardDuck implements Duck {
  @Override
  public void quack() {
    System.out.println("Quack");
  }

  @Override
  public void fly() {
    System.out.println("I'm flying");
  }
}

Adapter interface (turkey interface):

public interface Turkey {
  void gobble();
  void fly();
}

General implementation of the adaptee (turkey class):

public class WildTurkey implements Turkey {
  @Override
  public void gobble() {
    System.out.println("Gobble gobble");
  }

  @Override
  public void fly() {
    System.out.println("I'm flying a short distance");
  }
}

Adapter:

public class TurkeyAdapter implements Duck {
  private Turkey turkey;

  public TurkeyAdapter(Turkey turkey) {
    this.turkey = turkey;
  }

  @Override
  public void quack() {
    turkey.gobble();
  }

  @Override
  public void fly() {
    for (int i = 0; i < 5; i++) {
      turkey.fly();
    }
  }
}

Through the turkey adapter, we adapted the turkey into a duck . What needs to be explained here is that both the adapter mode and the decorator mode decorate the source object to achieve a certain effect. However, the difference between the adapter mode and the decorator mode is that the adapter mode adapts the source object so that it can conform to the A specific interface, and the decorator pattern is an extension of the functionality of the source object so that it can do more work.

The above is the detailed content of Example sharing about adapter pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn