设计模式——适配器模式

public interface Extends {
     void show();
     void show2();
}

public class SourceObj {
     void method() {
         System.out.println("父亲方法");
     }
}

//适配器对象

public class AdapterObject implements Extends{
    
     private SourceObj source;
     public AdapterObject(SourceObj source) {
         this.source = source;
     }
     public void method() {
         source.method();
     }
    
     @Override
     public void show() {
         // TODO Auto-generated method stub
         System.out.println("接口show()");
     }

    @Override
     public void show2() {
         // TODO Auto-generated method stub
         System.out.println("接口show2()");
     }
    
}

//+继承关系

public class Adapter extends SourceObj implements Extends{
    
     @Override
     public void show() {
         // TODO Auto-generated method stub
         System.out.println("接口show()");
     }

    @Override
     public void show2() {
         // TODO Auto-generated method stub
         System.out.println("接口show2()");
     }
    
}

//实现与调用

Adapter adapter = new Adapter();
         adapter.method();
         adapter.show();
         adapter.show2();
        
         SourceObj sourceObj = new SourceObj();
         AdapterObject aObject = new AdapterObject(sourceObj);
         aObject.method();
         aObject.show();
         aObject.show2();

posted @ 2020-03-25 18:26  小海_macro  阅读(94)  评论(0编辑  收藏  举报