java适配器模式
2015-05-09 10:43
295 查看
适配器模式是将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。
首先来看看类的适配器模式
核心思想:有一个Source类,拥有一个方法,待适配,通过Adapter类,将Source的功能扩展到目标接口Targetable里
Source类:
public class Source {
public void method1(){
System.out.println("original method");
}
}Targetable接口:
public interface Targetable {
/**与原类中的方法相同**/
public void method1();
/**新类的方法**/
public void method2();
}Adapter类:
public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is targetable method");
}
}
this is targetable method
这样Targetable接口的实现类就具有了Source类的功能。
public class AdapterTest {
public static void main(String[] args) {
//对象适配器
Wrapper wrapper = new Wrapper(new Source());
wrapper.method1();
wrapper.method2();
}
}输出结果:original
method
this is targetable method
对象适配器
基本思路和类的适配器模式相同,只是将Adapter类作修改,这次不继承Source类,而是持有Source类的实例,以达到解决兼容性的问题。
只需要修改Adapter类即可:
public class Wrapper implements Targetable {
private Source source;
public Wrapper(Source source){
this.source = source;
}
@Override
public void method1() {
source.method1();
}
@Override
public void method2() {
System.out.println("this is targetable method");
}
}
接口适配器
当一个接口中有很多方法,而我们只需要使用其中的某些方法,当我们写该接口的实现类时,必须实现所有的方法,这样就显得比较多余,因为并不是所有的方法都是我们所需要的,所以我们可以使用接口适配器,借助一个抽象类,该抽象类实现了接口,并实现了所有的方法,而我们只需要和该抽象类取得联系,用一个类去继承该抽象类重写我们需要的方法即可。
Sourceable接口:
public interface Sourceable {
public void method1();
public void method2();
}
Wrapper2抽象类:
public abstract class Wrapper2 implements Sourceable {
@Override
public void method1() {
}
@Override
public void method2() {
}
}SourceSub1类:
public class SourceSub1 extends Wrapper2 {
@Override
public void method1() {
System.out.println("the sourceable interface's first Sub1");
}
}SourceSub2类:
public class SourceSub2 extends Wrapper2{
@Override
public void method2() {
System.out.println("the sourceable interface's second Sub2");
}
}AdapterTest类:
public class AdapterTest {
public static void main(String[] args) {
//接口适配器
/*当你自己写的类想用接口中个别方法的时候(注意不是所有的方法),
*那么你就可以用一个抽象类先实现这个接口(方法体中为空),
*然后再用你的类继承这个抽象类,这样就可以达到你的目的了,
*如果你直接用类实现接口,那是所有方法都必须实现的
*/
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}输出结果:the
sourceable interface's first Sub1
the sourceable interface's second Sub2
最后总结一下三种适配器的使用场景:
类的适配器:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
对象适配器:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
接口适配器:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类重写我们所需要的方法即可。
首先来看看类的适配器模式
核心思想:有一个Source类,拥有一个方法,待适配,通过Adapter类,将Source的功能扩展到目标接口Targetable里
Source类:
public class Source {
public void method1(){
System.out.println("original method");
}
}Targetable接口:
public interface Targetable {
/**与原类中的方法相同**/
public void method1();
/**新类的方法**/
public void method2();
}Adapter类:
public class Adapter extends Source implements Targetable {
@Override
public void method2() {
System.out.println("this is targetable method");
}
}
public class AdapterTest { public static void main(String[] args) { //类的适配器 Targetable t = new Adapter(); t.method1(); t.method2(); } }返回结果:original method
this is targetable method
这样Targetable接口的实现类就具有了Source类的功能。
public class AdapterTest {
public static void main(String[] args) {
//对象适配器
Wrapper wrapper = new Wrapper(new Source());
wrapper.method1();
wrapper.method2();
}
}输出结果:original
method
this is targetable method
对象适配器
基本思路和类的适配器模式相同,只是将Adapter类作修改,这次不继承Source类,而是持有Source类的实例,以达到解决兼容性的问题。
只需要修改Adapter类即可:
public class Wrapper implements Targetable {
private Source source;
public Wrapper(Source source){
this.source = source;
}
@Override
public void method1() {
source.method1();
}
@Override
public void method2() {
System.out.println("this is targetable method");
}
}
接口适配器
当一个接口中有很多方法,而我们只需要使用其中的某些方法,当我们写该接口的实现类时,必须实现所有的方法,这样就显得比较多余,因为并不是所有的方法都是我们所需要的,所以我们可以使用接口适配器,借助一个抽象类,该抽象类实现了接口,并实现了所有的方法,而我们只需要和该抽象类取得联系,用一个类去继承该抽象类重写我们需要的方法即可。
Sourceable接口:
public interface Sourceable {
public void method1();
public void method2();
}
Wrapper2抽象类:
public abstract class Wrapper2 implements Sourceable {
@Override
public void method1() {
}
@Override
public void method2() {
}
}SourceSub1类:
public class SourceSub1 extends Wrapper2 {
@Override
public void method1() {
System.out.println("the sourceable interface's first Sub1");
}
}SourceSub2类:
public class SourceSub2 extends Wrapper2{
@Override
public void method2() {
System.out.println("the sourceable interface's second Sub2");
}
}AdapterTest类:
public class AdapterTest {
public static void main(String[] args) {
//接口适配器
/*当你自己写的类想用接口中个别方法的时候(注意不是所有的方法),
*那么你就可以用一个抽象类先实现这个接口(方法体中为空),
*然后再用你的类继承这个抽象类,这样就可以达到你的目的了,
*如果你直接用类实现接口,那是所有方法都必须实现的
*/
Sourceable source1 = new SourceSub1();
Sourceable source2 = new SourceSub2();
source1.method1();
source1.method2();
source2.method1();
source2.method2();
}
}输出结果:the
sourceable interface's first Sub1
the sourceable interface's second Sub2
最后总结一下三种适配器的使用场景:
类的适配器:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。
对象适配器:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。
接口适配器:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类重写我们所需要的方法即可。
相关文章推荐
- Java设计模式之适配器模式
- 深入浅出Java设计模式之适配器模式
- java/android 设计模式学习笔记(6)---适配器模式
- Java设计模式-适配器模式
- java结构型设计模式——适配器模式
- JAVA设计模式初探之适配器模式
- 【JAVA】设计模式之适配器模式(Adapter模式)的使用分析
- java设计模式之适配器模式
- Java设计模式---适配器模式(Adapter)
- Java设计模式之适配器模式
- Java 适配器模式
- Java:设计模式之适配器模式Adapter
- Java设计模式----初识适配器模式
- Java适配器模式
- Java设计模式-适配器模式
- Java模式(适配器模式)
- java 中的设计模式---适配器模式
- Java设计模式——适配器模式
- Java适配器模式(Adapter模式)
- java适配器模式