定义
java 适配器模式定义:
将两个不兼容的类纠合在一起使用,属于结构型模式,需要有 Adaptee(被适配者)和Adaptor(适配器)两个身份
说明
实现
package com.example.demo.adapter;
public class AdapterTest {
public static void main(String[] args) {
Target target = new AdapteeNew();
target.request();
Target target1= new Adapter();
target1.request();
}
}
// 原先的类
class Adaptee{
public void eat() {
System.out.println("原先普通类的方法");
}
}
class AdapteeNew implements Target{
public void run() {
System.out.println("新增加的类型");
}
@Override
public void request() {
// TODO Auto-generated method stub
System.out.println("新增加的类自己的方法");
}
}
interface Target{
public void request();
}
class Adapter extends Adaptee implements Target{
@Override
public void request() {
// TODO Auto-generated method stub
super.eat();
}
}