1. Class adapter pattern

When I first came in the morning, I saw my brother drinking water, and I suddenly felt bad. I said: Today the weather is a little gloomy, and it is estimated to rain again. It is wet everywhere, and even the design mode I just learned also feels like water injection!

The younger brother gave me one eye and said: How do you say this?

I said: Do you know the adapter-like pattern? As you probably know, adapters built from relationships between classes; It consists of three parts: existing resources, adaptive method interfaces, and target resources. The existing resource takes the target resource as its parent class and invokes the properties or methods of the parent class through methods that implement the interface. To achieve the existing resources to make the target resource operation. Let me show you a picture!

Look at the picture to talk: from the relationship, big gu inherited diga, big gu has the shaper, big gu can not fight the monster, Diga can fight the monster. Big ancient want to play monster, need to open the transformation device through the realization, in order to call dija’s various abilities, the realization of the monster.

/** * Dija Altman **@author czy
 * @date2021/6/18 * /
public class UltramanTiga {

    public  void action(a){
        System.out.println("Deja smashes the universe boy with cosmic rays.");
        System.out.println("Deja deals 100 damage to the monster."); }}/** * Big Goo * Big goo needs a Shape-shifter ** to transform into Diga@author czy
 * @date2021/6/18 * /
public class BigGu extends UltramanTiga implements ShapeShifter{

    @Override
    public void becomeBigPerson(a) {
        this.action(); }}/** ** Shaper **@author czy
 * @date2021/6/18 * /
public interface ShapeShifter {
    /** * becomes a giant */
    void becomeBigPerson(a);
}

/** * Come and see ultraman fight the monster **@author czy
 * @date2021/6/18 * /
public class Test {
    public static void main(String[] args) {
            // I have a big gu, I want to fight monsters
            BigGu bigGu = new BigGu();
            // I want a diggabigGu.becomeBigPerson(); }}Copy the code

Younger brother: The inheritance coupling is a bit high! It should not be applicable in actual production.

Me: Yes, inheriting the parent class implementation adaptively coupled

I: yes, obviously inherit the parent class, call the parent class method on the line, this mode also through the implementation of the interface, through the implementation of the interface method to call the parent class interface, feel redundant, is completely in the water experience, therefore, I say it is in the water!

2. Object adaptation mode

Brother: Is there a good solution?

Me: Just change the inheritance to a combination. Everyone has diga in them and needs to inspire them through the light of hope. This is the object adaptation pattern. Kaka rabbit!!

/** * Dija Ultraman Ultimate edition **@author czy
 * @date2021/6/18 * /
public class UltramanTiga {

    public  void action(a){
        System.out.println("Deja smashes the universe boy with cosmic rays.");
        System.out.println("Deja deals 100 damage to the monster."); }}/** * Everyone * everyone has diga in their heart of faith, operating diga to fight monsters through the light of hope *@author czy
 * @date2021/6/21 * /
public class EveryBody implements Light{
    private UltramanTiga ultramanTiga;

    public EveryBody(UltramanTiga ultramanTiga) {
        this.ultramanTiga = ultramanTiga;
    }

    @Override
    public void becomeBigPerson(a) { ultramanTiga.action(); }}/** ** light of Hope ** ADAPTS interface **@author czy
 * @date2021/6/21 * /
public interface Light {
    /** * becomes a giant */
    void becomeBigPerson(a);
}

/** * Everyone is a Dija **@author czy
 * @date2021/6/21 * /
public class Test {
    public static void main(String[] args) {
        EveryBody body = new EveryBody(newUltramanTiga()); body.becomeBigPerson(); }}Copy the code

Look at multiple interfaces here to increase the amount of code! Is it water!

Brother: Water! Obviously can directly do not use the interface!

My heart snicker, the surface is steady as old dog: this is face interface programming SAO years! Remember the seven principles? Rely on the inversion principle, interface oriented, not implementation oriented, so I’m pouring water!