dimanche 21 avril 2019

How to use two-way adapter and pluggable adapters in Java?

Recently, I have read the "GOF" book. In adapter chapter, the book mentioned Pluggable adapters, I read the chapter over and over, but I can't understand it because of its example in Smalltalk. What's more, it also mentioned the two-way adapter which I have ever met several times, but no books gave me an example code. I don't know whether my understanding is right. This is my code:

public interface Hero {
    void save();
}

public interface Evil {
    void destory();
}

public class Adapter implements Hero, Evil {
    private Hero hero;
    private Evil evil;

    public Adapter(Hero hero) {
        this.hero = hero;
    }

    public Adapter(Evil evil) {
        this.evil = evil;
    }

    @Override
    public void destory() {
        hero.save();
    }

    @Override
    public void save() {
        evil.destory();
    }
}

In conclusion, what's I want to know is whether my code is right or not and how to use a Pluggable adapter in Java. Anyway, Your answer will be welcome all the time.

Aucun commentaire:

Enregistrer un commentaire