适配器模式实例

问题描述

某OA系统需要提供一个加密模块,将用户机密信息(如:用户口令、邮箱等)加密之后再存储到数据库中,系统已经定义好了数据库操作类。为了提高开发效率,系统需要重用已有的加密算法,这些算法封装在一些由第三方提供的类中,有些甚至没有源代码,试用适配器模式设计该加密模块,实现在不修改现有类的基础上重用第三方的加密方法。

结构图

在这里插入图片描述

编程实现

Encode类

public interface Encode {
    String encodePwd(String password);
}

适配者类(Adaptee)

public class Adaptee {
    public String encodeAPI(String password){
        return password.toUpperCase();
    }
}

适配器类(Adapter)

public class Adapter implements Encode{
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public String encodePwd(String password) {
        return adaptee.encodeAPI(password);
    }
}

客户端类

public class Client {
    public static void main(String[] args) {
        Encode encode=new Adapter(new Adaptee());
        String result=encode.encodePwd("hello");
        System.out.println(result);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值