有一个接口需要实现,但是我们现成的对象都不满足,需要加一层适配器来进行适配。
默认适配器模式
我们用 Appache commons-io 包中的 FileAlterationListener 做例子,此接口定义了很多的方法,用于对文件或文件夹进行监控,一旦发生了对应的操作,就会触发相应的方法。
1 2 3 4 5 6 7 8 9 10
| public interface FileAlterationListener { void onStart(final FileAlterationObserver observer); void onDirectoryCreate(final File directory); void onDirectoryChange(final File directory); void onDirectoryDelete(final File directory); void onFileCreate(final File file); void onFileChange(final File file); void onFileDelete(final File file); void onStop(final FileAlterationObserver observer); }
|
此接口的一大问题是抽象方法太多了,如果我们要用这个接口,意味着我们要实现每一个抽象方法,如果我们只是想要监控文件夹中的文件创建和文件删除事件,可是我们还是不得不实现所有的方法,很明显,这不是我们想要的。
所以,我们需要下面的一个适配器,它用于实现上面的接口,但是所有的方法都是空方法,这样,我们就可以转而定义自己的类来继承下面这个类即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class FileAlterationListenerAdapter implements FileAlterationListener {
public void onStart(final FileAlterationObserver observer) { }
public void onDirectoryCreate(final File directory) { }
public void onDirectoryChange(final File directory) { }
public void onDirectoryDelete(final File directory) { }
public void onFileCreate(final File file) { }
public void onFileChange(final File file) { }
public void onFileDelete(final File file) { }
public void onStop(final FileAlterationObserver observer) { } }
|
比如我们可以定义以下类,我们仅仅需要实现我们想实现的方法就可以了:
1 2 3 4 5 6 7 8 9 10 11
| public class FileMonitor extends FileAlterationListenerAdapter { public void onFileCreate(final File file) { doSomething(); }
public void onFileDelete(final File file) { doSomething(); } }
|
对象适配器模式
来看一个《Head First 设计模式》中的一个例子,我稍微修改了一下,看看怎么将鸡适配成鸭,这样鸡也能当鸭来用。因为,现在鸭这个接口,我们没有合适的实现类可以用,所以需要适配器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public interface Duck { public void quack(); public void fly(); }
public interface Cock { public void gobble(); public void fly(); }
public class WildCock implements Cock { public void gobble() { System.out.println("咕咕叫"); } public void fly() { System.out.println("鸡也会飞哦"); } }
|
鸭接口有 fly() 和 quare() 两个方法,鸡 Cock 如果要冒充鸭,fly() 方法是现成的,但是鸡不会鸭的呱呱叫,没有 quack() 方法。这个时候就需要适配了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class CockAdapter implements Duck {
Cock cock; public CockAdapter(Cock cock) { this.cock = cock; }
@Override public void quack() { cock.gobble(); }
@Override public void fly() { cock.fly(); } }
|
客户端调用很简单了:
1 2 3 4 5 6 7 8 9
| public class Main { public static void main(String[] args) { Cock wildCock = new WildCock(); Duck duck = new CockAdapter(wildCock); ... } }
|
类适配器模式
通过继承的方法,适配器自动获得了所需要的大部分方法。
实现的适配器:
1 2 3 4 5 6 7
| public class CockAdapter extends WildCock implements Duck { @Override public void quack() { gobble(); } }
|
客户端调用:
1 2 3 4 5 6
| public class Main { public static void main(String[] args) { Duck duck = new CockAdapter(); ... } }
|
参考