适配器模式(Adapter模式)

适配器模式通过接口和继承的方式来实现在适配类中调用目标类

  • 类适配器
    在这里插入图片描述
    在这里插入图片描述
public class ClassAdapterTest {
    public static void main(String[] args) {
        System.out.println("类适配器模式测试:");
        //面向接口编程
        Target target = new ClassAdapter();
        target.request();
    }
}
public interface Target {
    void request();
}
public class Adaptee {
    void specificRequest(){
        System.out.println("适配者中的业务代码被调用");
    }
}
public class ClassAdapter extends Adaptee implements Target {
    @Override
    public void request(){
        super.specificRequest();
    }
}
  • 对象适配器
    在这里插入图片描述
public class ObjectAdapterTest {
    public static void main(String[] args) {
        System.out.println("对象适配器模式测试:");
        //面向接口编程
        Adaptee adaptee = new Adaptee();
        Target target = new ObjectAdapter(adaptee);
        target.request();
    }
}
public interface Target {
    void request();
}
public class Adaptee {
    void specificRequest(){
        System.out.println("适配者中的业务代码被调用");
    }
}
public class ObjectAdapter implements Target {
    Adaptee adaptee;

    ObjectAdapter(Adaptee adaptee){
        this.adaptee=adaptee;
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}
  • 默认适配器
public class DefaultAdapterTest {
    public static void main(String[] args) {
        /*
        默认适配器
         */
        DefaultAdapterSub1 sub1 = new DefaultAdapterSub1();
        DefaultAdapterSub2 sub2 = new DefaultAdapterSub2();

        sub1.operation1();
        System.out.println("----------");
        sub1.operation2();
        System.out.println("----------");
        sub2.operation1();
        System.out.println("----------");
        sub2.operation2();
    }
}
public interface Target {
   void operation1();
   void operation2();
}
public abstract class DefaultAdapter implements Target{
    @Override
    public void operation1() {

    }

    @Override
    public void operation2() {

    }
}
public class DefaultAdapterSub1 extends DefaultAdapter{
    @Override
    public void operation1() {
        System.out.println("重写了operation1方法");
    }
}
public class DefaultAdapterSub2 extends DefaultAdapter{
    @Override
    public void operation2() {
        System.out.println("重写了operation2方法");
    }
}
  • 双向适配器
    在这里插入图片描述
public class ObjectAdapterTest {
    public static void main(String[] args) {
        System.out.println("目标通过双向适配器访问适配者:");
        //面向接口编程
        TwoWayAdaptee adaptee = new AdapteeRealize();
        TwoWayTarget target = new TwoWayAdapter(adaptee);
        target.request();
        System.out.println("-------------------");
        System.out.println("适配者通过双向适配器访问目标:");
        target = new TargetRealize();
        adaptee = new TwoWayAdapter(target);
        adaptee.specificRequest();
    }
}
public interface TwoWayTarget {
    void request();
}
public interface TwoWayAdaptee {
    void specificRequest();
}
public class TargetRealize implements TwoWayTarget {
    @Override
    public void request() {
        System.out.println("目标代码被调用!");
    }
}
public class AdapteeRealize implements  TwoWayAdaptee {
    @Override
    public void specificRequest() {
        System.out.println("适配者代码被调用!");
    }
}
public class TwoWayAdapter implements TwoWayTarget, TwoWayAdaptee {
    private TwoWayTarget target;
    private TwoWayAdaptee adaptee;

    public TwoWayAdapter(TwoWayTarget target) {
        this.target = target;
    }

    public TwoWayAdapter(TwoWayAdaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }

    @Override
    public void specificRequest() {
        target.request();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值