适配器模式

来源:互联网 发布:网络爬虫工具 绿色版 编辑:程序博客网 时间:2024/05/29 14:29

将一个类的接口转化为另一个客户需要的接口,Adapter模式是的原来本不兼容的那些类型可以一起工作,从实现上来说,适配器实际上就是创建适配器类实现目标接口,适配器中持有被适配类的对象,在适配器中使用此对象进行处理
这里写图片描述

代码实现:

//目标接口public interface Targert {    public void request();}//待适配对象public class Adaptee {    public void actualRequest(){        System.out.println("actual request");    }}//适配器public class Adapter implements Targert {    //持有待是被对象    Adaptee adaptee = new Adaptee();    @Override    public void request() {        adaptee.actualRequest();    }}//客户端public class Client {    public static void main(String[] args) {        Targert targert = new Adapter();        targert.request();    }}

适配器一般在两个类所做的事情相同或者相似,但是具有不同接口的时候使用。

原创粉丝点击