1

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.

xun yan
  • 69
  • 5
  • Possible duplicate of [Can anybody explain the concept of pluggable adapter to me with good example?](https://stackoverflow.com/questions/449424/can-anybody-explain-the-concept-of-pluggable-adapter-to-me-with-good-example) – Sharon Ben Asher Apr 22 '19 at 06:26
  • 1
    You'll want null-pointer checks and dispatch to either the `hero` or the `evil`, depending on which one is set. – Thilo Apr 22 '19 at 08:21
  • Possible duplicate of [How to implement Pluggable Adapter design pattern in Java](https://stackoverflow.com/questions/55574759/how-to-implement-pluggable-adapter-design-pattern-in-java) – Matt Timmermans Apr 22 '19 at 12:42

0 Answers0