"Illustrates a design mode" mode study notes 1-2 Adapter

table of Contents

Adapter i.e. the adapter can be assimilated to a voltage of 220V into the voltage of 5V cell phone charger, serves conversion.

  • A clear concept:
    the Adaptee : the fitter, i.e. the power supply voltage 220v
    Adapter : adapter, i.e., cell phone chargers
    the Target : target adaptation, i.e. the output voltage of 5V
    Client : request fitter, i.e. phone

  • Adapter model has two implementations:
  1. Use inherited adapter.

Class Diagram

Code

//Adaptee
public class Banner {
    private String string;
    public Banner(String string) {
        this.string = string;
    }
    public void showWithParen() {
        System.out.println("("+string+")");
    }
    public void showWithAster() {
        System.out.println("*" + string + "*");
    }
}

//Target
public interface Print {
    public abstract void printWeak();
    public abstract void printStrong();
}

//Adapter
public class PrintBanner extends Banner implements Print {
    public PrintBanner(String string) {
        super(string);
    }
    @Override
    public void printWeak() {
        showWithParen();
    }
    @Override
    public void printStrong() {
        showWithAster();
    }
}

//Client
public static void main(String[] args) {
        Print p = new PrintBanner("Hello");
        p.printWeak();
        p.printStrong();
}
  1. Use adapter delegate.

Class Diagram

Code

//Adaptee
public class Banner {
    private String string;
    public Banner(String string) {
        this.string = string;
    }
    public void showWithParen() {
        System.out.println("("+string+")");
    }
    public void showWithAster() {
        System.out.println("*" + string + "*");
    }
}

//Target
public abstract class Print {
    public abstract void printWeak();
    public abstract void printStrong();
}

//Adapter
public class PrintBanner extends Print {
    private Banner banner;
    public PrintBanner(String string) {
        this.banner = new Banner(string);
    }
    public void printWeak() {
        banner.showWithParen();
    }
    public void printStrong() {
        banner.showWithAster();
    }
}

//Client
public static void main(String[] args) {
        Print p = new PrintBanner("Hello");
        p.printWeak();
        p.printStrong();
}
  • The difference between the two implementations
    Adapter Target be implemented according to different types, respectively. If the Target is an interface that inherits Adaptee Adapter, Target's method of using Adaptee methods. If Target is an abstract class that is created Adaptee Adapter their own internal objects, inheritance Target class, call the object's methods Target Adaptee class methods.

  • Why this pattern
    many times, we are not starting from scratch programming, to try to reuse existing well-tested class. This time using the Adapter pattern for this class adaptation, borrowing existing methods of this class to develop new features, reduce testing burden.
    There are more scenarios, such as increased compatibility with older versions. But I still can not fully understand.

Guess you like

Origin www.cnblogs.com/qianbixin/p/10992678.html