• Thank you very much for reading this article. Welcome [👍 like] [⭐ collection] [📝 comment] ~
  • It’s not hard to give up, but it must be cool to hold on! I hope we can all make a little progress every day! 🎉

What is the adapter pattern

Here’s what the encyclopedia explains.

In computer programming, the adapter pattern (sometimes called wrapper style or wrapper) ADAPTS the interface of a class to what the user expects. An adaptation allows classes that would normally not work together because of interface incompatibilities to work together by wrapping the class’s own interface in an existing class.

There are two types of adapter patterns:

  • Adapter-like pattern:

In this adapter pattern, the adapter inherits from the classes it implements (generic multiple inheritance).

  • Object adapter pattern:

In this adapter pattern, the adapter holds an instance of the class it wraps around. In this case, the adapter calls the wrapped object.

Design patterns have nothing to do with the programming language, but the master still uses the Java language for practical examples.


Class adaptor pattern

  • Source Adapee role: The interface that needs to be adapted now.
  • Target role: This is the desired interface. Note: Since we are talking about the class adapter pattern, the target cannot be a class.
  • Adapter (Adaper) Role: The adapter class is the core of this pattern. The adapter converts the source interface into the target interface. Obviously, this role cannot be an interface, but must be a concrete class.

The source (Adapee) role

Two masters like dogs, so raise a dog, he sometimes bark.

package com.secondgod.adapter;

/** ** dog **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Dog {
    /** * make a sound */
    public void makeSound(a) {
        System.out.println("Dog: Woof woof..."); }}Copy the code

Target role

We talk to our friends.

package com.secondgod.adapter;

/** * friend **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public interface IFriend {
    /** * speak */
    void speak(a);
}
Copy the code

Adapter (Adaper) role

After a while, the second master took the dog as a friend, thinking it was not barking, but talking.

package com.secondgod.adapter;

/** ** dog friend **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class DogFriend extends Dog implements IFriend {
    /** ** says */
    @Override
    public void speak(a) {
        super.makeSound(); }}Copy the code

Let’s test our ability to speak to our canine friends.

package com.secondgod.adapter;

/** * people **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Person {
    /** * chat with friends **@param friend
     */
    public void speakTo(IFriend friend) {
        System.out.println(Man: friend, what are you doing?);
        friend.speak();
    }

    public static void main(String[] args) {
        Person  person = new Person();
        IFriend friend = newDogFriend(); person.speakTo(friend); }}Copy the code

Two master said, the dog barked, we really like a chat.


Consequences of adding the source Adapee role

One day, the second husband raised a cat.

package com.secondgod.adapter;

/** ** Cat **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Cat {
    /** * make a sound */
    public void makeSound(a) {
        System.out.println("Cat: Meow meow meow..."); }}Copy the code

After a few days, two master and cat also became friends. Add a cat friend to the list.

package com.secondgod.adapter;

/** * Cat friend **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class CatFriend extends Cat implements IFriend {
    /** ** says */
    @Override
    public void speak(a) {
        super.makeSound(); }}Copy the code

Two boss and dog friends, cat friends chat.

package com.secondgod.adapter;

/** * people **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Person {
    /** * chat with friends **@param friend
     */
    public void speakTo(IFriend friend) {
        System.out.println(Man: friend, what are you doing?);
        friend.speak();
    }

    public static void main(String[] args) {
        Person  person = new Person();
        IFriend dogFriend = new DogFriend();
        IFriend catFriend = newCatFriend(); person.speakTo(dogFriend); person.speakTo(catFriend); }}Copy the code

In the future, if you have other animal friends, you need to add adapter classes. Is there any way to make it generic?


Object for the adapter schema

I wish I could have a way to make friends with all kinds of animals instead of having to add an adapter every time I have a new animal friend.


Added an animal interface

package com.secondgod.adapter;

/** * animal **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public interface IAnimal {

    /** * make a sound */
    void makeSound(a);
}
Copy the code

Let the cat and dog of the source (Adapee) role implement the animal interface

package com.secondgod.adapter;

/** ** dog **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Dog implements IAnimal {
    /** * make a sound */
    public void makeSound(a) {
        System.out.println("Dog: Woof woof..."); }}Copy the code
package com.secondgod.adapter;

/** ** Cat **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Cat implements IAnimal {
    /** * make a sound */
    public void makeSound(a) {
        System.out.println("Cat: Meow meow meow..."); }}Copy the code

All Things Anthropomorphic Adapter (Adaper) character

package com.secondgod.adapter;

/** * Everything anthropomorphic adapter **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class AnimalFriendAdaper implements IFriend {
    /** ** anthropomorphic animal friend */
    private IAnimal animal;

    public AnimalFriendAdaper(IAnimal animal) {
        this.animal = animal;
    }

    @Override
    public void speak(a) { animal.makeSound(); }}Copy the code

Test our anthropomorphic adapter for all things.

package com.secondgod.adapter;

/** * people **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class Person {
    /** * chat with friends **@param friend
     */
    public void speakTo(IFriend friend) {
        System.out.println(Man: friend, what are you doing?);
        friend.speak();
    }

    public static void main(String[] args) {
        / / one
        Person  person = new Person();
        / / a dog
        IAnimal dog = new Dog();
        / / a cat
        IAnimal cat = new Cat();
        // All things are human
        person.speakTo(new AnimalFriendAdaper(dog));
        person.speakTo(newAnimalFriendAdaper(cat)); }}Copy the code

Too good. It’s so much easier to be friends with animals. Because of the anthropomorphic adapter of all things.


Default adaptation mode


Target roles add behavior declarations

One day, the criteria for a friend changed. You have to know how to make bricks.

package com.secondgod.adapter;

/** * friend **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public interface IFriend {
    /** * speak */
    void speak(a);

    /** ** code */
    void coding(a);
}
Copy the code

The adapter (Adaper) role must be followed by increased behavior implementations

Modified anthropomorphic adapter for all things

package com.secondgod.adapter;

/** * Everything anthropomorphic adapter **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class AnimalFriendAdaper implements IFriend {
    /** ** anthropomorphic animal friend */
    private IAnimal animal;

    public AnimalFriendAdaper(IAnimal animal) {
        this.animal = animal;
    }

    @Override
    public void speak(a) {
        animal.makeSound();
    }

    @Override
    public void coding(a) {
        System.out.println("Animal: Smiles and shakes his head..."); }}Copy the code

Default adapter

Two masters want to make friends with animals, but do not want to consider how they code brick, after two masters if they make friends with plants, also have to realize code brick behavior for plant friends, annoying oh. So let’s do a default empty implementation.

package com.secondgod.adapter;

/** * Default adapter **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public abstract class FriendAdaper implements IFriend {
    @Override
    public void speak(a) {}@Override
    public void coding(a) {}}Copy the code
package com.secondgod.adapter;

/** * Everything anthropomorphic adapter **@authorTwo master's white hat https://le-yi.blog.csdn.net/ */
public class AnimalFriendAdaper extends FriendAdaper {
    /** ** anthropomorphic animal friend */
    private IAnimal animal;

    public AnimalFriendAdaper(IAnimal animal) {
        this.animal = animal;
    }

    @Override
    public void speak(a) { animal.makeSound(); }}Copy the code

With one more default implementation, we do not need to implement the code brick behavior for the everything adapter.

The intent of the adapter pattern is to change the source interface so that the target interface is compatible. The purpose of default adaptation is slightly different, providing a mediocre implementation to facilitate the creation of a non-mediocre adapter class.

At any time when you are not ready to implement all the methods of an interface, you can use the “default adaptation pattern” to make an abstract class that gives a banal concrete implementation of all the methods. In this way, subclasses that inherit from this abstract class do not have to implement all the methods.