欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > 设计模式4 适配器 (adapter)

设计模式4 适配器 (adapter)

2024/10/28 5:01:52 来源:https://blog.csdn.net/liu_chunhai/article/details/143171045  浏览:    关键词:设计模式4 适配器 (adapter)

一句话,适配器按照客户的需求,    适配当前已有的接口。

目标接口:reqeust()

public interface Target {void request();  //this is client needed interface
}

已有接口:specificRequest

package com.example.adapter;import android.util.Log;public class Adaptee {public void specificRequest(){Log.d ("LIU","that is Adaptee specificRequest");}
}

两者的接口功能是一样的,但是需要将已有的接口 适配成符合客户要求的新接口。

public class Adapter implements Target {private Adaptee adaptee;public Adapter(Adaptee adaptee) {   //对象适配器模式,传入对象,并调用对象的 接口this.adaptee = adaptee;}@Overridepublic void request() {adaptee.specificRequest();}
}

调用实现:

        Adaptee adaptee = new Adaptee();Adapter adapter = new Adapter(adaptee);adapter.request();

结果:

2024-10-22 23:05:26.770 12570-12570/com.example.adapter D/LIU: that is Adaptee specificRequest

参考:百度安全验证

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com