JavaJava 杂谈架构算法设计模式和编程理论

结构类——适配器模式

2019-05-29  本文已影响63人  Jason_Sam

适配器模式

适配器模式实践

源接口

    public interface IAdaptee {
        public void origin();
    }

目标接口

    public interface ITarget {
        public void expend();
    }

适配器角色

    public class Adapter implements IAdaptee, ITarget{
        @Override
        public void expend() {
            System.out.println("I am a target!");
        }
        
        @Override
        public void origin() {
            System.out.println("I am a adaptee!");
            expend();
        }
    }

客户端类

public class client {
    public static void main(String[] args) {
        IAdaptee adaptee = new concreteAdaptee();
        adaptee.origin();
        Adapter adapter = new Adapter();
        adapter.origin();
    }
}

源代码

上一篇 下一篇

猜你喜欢

热点阅读