78 lines
1.9 KiB
Java
78 lines
1.9 KiB
Java
## 1. 适配器(Adapter)
|
||
|
||
### Intent
|
||
|
||
把一个类接口转换成另一个用户需要的接口。
|
||
|
||
<div align="center"> <img src="pics/3d5b828e-5c4d-48d8-a440-281e4a8e1c92.png"/> </div><br>
|
||
|
||
### Class Diagram
|
||
|
||
<div align="center"> <img src="pics/ff5152fc-4ff3-44c4-95d6-1061002c364a.png"/> </div><br>
|
||
|
||
### Implementation
|
||
|
||
鸭子(Duck)和火鸡(Turkey)拥有不同的叫声,Duck 的叫声调用 quack() 方法,而 Turkey 调用 gobble() 方法。
|
||
|
||
要求将 Turkey 的 gobble() 方法适配成 Duck 的 quack() 方法,从而让火鸡冒充鸭子!
|
||
|
||
```java
|
||
public interface Duck {
|
||
void quack();
|
||
}
|
||
```
|
||
|
||
```java
|
||
public interface Turkey {
|
||
void gobble();
|
||
}
|
||
```
|
||
|
||
```java
|
||
public class WildTurkey implements Turkey {
|
||
@Override
|
||
public void gobble() {
|
||
System.out.println("gobble!");
|
||
}
|
||
}
|
||
```
|
||
|
||
```java
|
||
public class TurkeyAdapter implements Duck {
|
||
Turkey turkey;
|
||
|
||
public TurkeyAdapter(Turkey turkey) {
|
||
this.turkey = turkey;
|
||
}
|
||
|
||
@Override
|
||
public void quack() {
|
||
turkey.gobble();
|
||
}
|
||
}
|
||
```
|
||
|
||
```java
|
||
public class Client {
|
||
public static void main(String[] args) {
|
||
Turkey turkey = new WildTurkey();
|
||
Duck duck = new TurkeyAdapter(turkey);
|
||
duck.quack();
|
||
}
|
||
}
|
||
```
|
||
|
||
### JDK
|
||
|
||
- [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29)
|
||
- [java.util.Collections#list()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#list-java.util.Enumeration-)
|
||
- [java.util.Collections#enumeration()](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#enumeration-java.util.Collection-)
|
||
- [javax.xml.bind.annotation.adapters.XMLAdapter](http://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html#marshal-BoundType-)
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|