首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

java设计模式(5)-适配器模式

适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题

上篇文章我讲完了5种创建型模式,这章开始,我将讲下7种结构型模式:适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享元模式。其中对象的适配器模式是各种模式的起源,我们看下面的图:

(1) 类的适配器模式

代码语言:javascript
复制
public class Source {  
    public void sourceMethod() {  
        System.out.println("this is Source Method!");  
    }  
}  
代码语言:javascript
复制
public interface Target {  
    /* 与原类中的方法相同 */  
    public void sourceMethod();  
  
    /* 新类的方法 */  
    public void targetMethod();  
}  
代码语言:javascript
复制
public class Adapter extends Source implements Target {  
  
    @Override  
    public void targetMethod() {  
        System.out.println("this is the Target Method!");  
    }  
}  

测试类:这样Target接口的实现类就具有了Source类的功能。

代码语言:javascript
复制
public class Test {  
  
    public static void main(String[] args) {  
        Target target = new Adapter();  
        target.sourceMethod();  /* this is Source Method! */   
        target.targetMethod();  /* this is the Target Method! */  
    }  
}

(2) 对象的适配器模式

只需要修改上述的Adapter

代码语言:javascript
复制
public class Wrapper implements Target {  
  
    private Source source;  
      
    public Wrapper(Source source){  
        super();  
        this.source = source;  
    }  
    @Override  
    public void targetMethod() {  
        System.out.println("this is the Target Method!");  
    }  
  
    @Override  
    public void sourceMethod() {  
        source.sourceMethod();  
    }  
}  

测试类:输出和类的适配器模式是一样的,但是适配的方法不同

代码语言:javascript
复制
public class AdapterTest {  
  
    public static void main(String[] args) {  
        Source source = new Source();  
        Target target = new Wrapper(source);  
        target.sourceMethod();  /* this is Source Method! */   
        target.targetMethod();  /* this is the Target Method! */
    }  
}  

(3) 接口的适配器模式

总体来说就是,原类设计成接口,用抽象了类去实现接口,业务操作类去继承抽象类并实现具体业务即可。

在实现业务逻辑的时候很方便

代码语言:javascript
复制
public interface Source {  
    public void sourceMethod_1() {  
        System.out.println("this is Source Method_1!");  
    }  
    public void sourceMethod_2() {  
        System.out.println("this is Source Method_2!");  
    } 
}  
代码语言:javascript
复制
public abstract class WrapperInte implements Source{  
      
    public void sourceMethod_1(){}  
    public void sourceMethod_2(){}  
}  
代码语言:javascript
复制
public class SourceSup_1 extends WrapperInte {  
    public void sourceMethod_1(){  
        System.out.println("the source interface's first SourceSup_1!");  
    }  
}  
代码语言:javascript
复制
public class SourceSup_2 extends WrapperInte {  
    public void sourceMethod_2(){  
        System.out.println("the source interface's second SourceSup_2!");  
    }  
}  

测试类:

代码语言:javascript
复制
public class Test {  
  
    public static void main(String[] args) {  
        Source source1 = new SourceSup_1();  
        Source source2 = new SourceSup_2();  
          
        /* the source interface's first SourceSup_1! */  
        source1.sourceMethod_1(); 
        /* this is Source Method! */  
        source1.sourceMethod_2();  
        source2.sourceMethod_1();  
        source2.sourceMethod_2();  
    }  
} 
  1. 类的适配器模式:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
  2. 对象的适配器模式:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
  3. 接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。
下一篇
举报
领券