设计模式——适配器模式

栏目: 后端 · 发布时间: 6年前

适配器模式也叫做包装模式;就是把内部结构包装(适配)成用户期待的格式,使得可以兼容使用

通过继承方式使用适配器模式

设计模式——适配器模式
/**
 * @author maikec
 * @date 2019/5/9
 */
public interface Print {
    /**
     * 打印带括号的消息
     */
    void printParen();

    /**
     * 打印带*号的消息
     */
    void printAster();
}

/**
 * 实际使用中的类
 * @author maikec
 * @date 2019/5/9
 */
public class Banner {
    private final String msg;
    public Banner(String msg){
        this.msg = msg;
    }
    public void showWithParen(){
        System.out.println( "(" + msg + ")" );
    }
    public void showWithAster(){
        System.out.println( "*" + msg + "*" );
    }
}

/**
 * @author maikec
 * @date 2019/5/9
 */
public class PrintBannerAdapter extends Banner implements Print {

    public PrintBannerAdapter(String msg) {
        super( msg );
    }

    @Override
    public void printParen() {
        showWithParen();
    }

    @Override
    public void printAster() {
        showWithAster();
    }
}

/**
 * @author maikec
 * @date 2019/5/9
 */
public class AdapterExtendDemo {
    public static void main(String[] args) {
        Print print = new PrintBannerAdapter( "hello adapter" );
        print.printAster(  );
        print.printParen();
    }
}

复制代码

通过引用使用适配器模式

设计模式——适配器模式
/**
 * 需求
 * @author maikec
 * @date 2019/5/9
 */
public abstract class Print {
    /**
     * 打印带括号的消息
     */
    public abstract void printParen();

    /**
     * 打印带*号的消息
     */
    public abstract void printAster();

    public void i(){

    }
}

/**
 * 实际使用中的类
 * @author maikec
 * @date 2019/5/9
 */
public class Banner {
    private final String msg;
    public Banner(String msg){
        this.msg = msg;
    }
    public void showWithParen(){
        System.out.println( "(" + msg + ")" );
    }
    public void showWithAster(){
        System.out.println( "*" + msg + "*" );
    }
}

/**
 * @author maikec
 * @date 2019/5/9
 */
public class PrintBannerAdapter extends Print {
    private Banner banner;

    public PrintBannerAdapter(Banner banner){
        this.banner = banner;
    }
    @Override
    public void printParen() {
        banner.showWithParen();
    }

    @Override
    public void printAster() {
        banner.showWithAster();
    }
}

/**
 * @author maikec
 * @date 2019/5/9
 */
public class AdapterReferenceDemo {
    public static void main(String[] args) {
        Print print = new PrintBannerAdapter( new Banner( "Hello Adapter" ) );
        print.printAster();
        print.printParen();
    }
}

复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换