您的位置:首页 > 其它

适配器模式

2016-03-22 18:06 232 查看

适配器模式分为三种:

1.类的适配器模式

2.对象的适配器模式

3.接口的适配器模式

/**
*
* @author: muyichun
* @date  : 2016年3月22日16:11:11
* @function: 适配器模式
*/
public class Main{

public static void main(String[] args) {
Adapter target = new Adapter();
target.method1();
target.method2();
}
}

class Source{
public void method1(){
System.out.println("method1");
}
}
interface Targetable{
public void method1();  //原类中的方法相同

public void method2();  //新类的方法
}

class Adapter extends Source implements Targetable{

@Override
public void method2() {
System.out.println("method2");
}

}


此代码为类的适配器模式,另外两种类同。 对象适配器模式:将Source作为Adapter的参数传递进去。 接口适配器模式:类似于even事件里的监听adapter类,达到实现那些不必要的方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: