代码文件目录:
Example1:
Duck
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public interface Duck {
public void quack();
public void fly();
}
DuckTestDrive
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class DuckTestDrive {
public static void main(String[] args) {
Duck duck = new MallardDuck();
Turkey turkey = new WildTurkey();
Duck duckAdapter = new TurkeyAdapter(turkey);
System.out.println("\nTurkeys say:...");
turkey.gobble();
turkey.fly();
System.out.println("\nDucks say:...");
testDuck(duck);
System.out.println("\nTurkeyAdapters say:...");
testDuck(duckAdapter);
}
static void testDuck(Duck duck){
duck.quack();
duck.fly();
}
}
MallardDuck
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class MallardDuck implements Duck{
@Override
public void quack() {
System.out.println("Quack!");
}
@Override
public void fly() {
System.out.println("I'm flying!");
}
}
Turkey
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public interface Turkey {
public void gobble();
public void fly();
}
TurkeyAdapter
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class TurkeyAdapter implements Duck{
Turkey turkey;
public TurkeyAdapter(Turkey turkey){
this.turkey = turkey;
}
@Override
public void quack() {
this.turkey.gobble();
}
@Override
public void fly() {
for(int i = 0;i<5;++i){
this.turkey.fly();
}
}
}
WildTurkey
package Chapter7_AdapterAndFacadePattern.Adapter.Example1;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class WildTurkey implements Turkey{
@Override
public void gobble() {
System.out.println("Gobble gobble!");
}
@Override
public void fly() {
System.out.println("I'm flying a short distance!");
}
}
Example2:
EnumerationIterator
package Chapter7_AdapterAndFacadePattern.Adapter.Example2;
import java.util.Enumeration;
import java.util.Iterator;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class EnumerationIterator implements Iterator<Object> {
Enumeration<?> enumeration;
public EnumerationIterator(Enumeration<?> enumeration){
this.enumeration = enumeration;
}
public boolean hasNext(){
return this.enumeration.hasMoreElements();
}
public Object next(){
return this.enumeration.nextElement();
}
public void remove(){
throw new UnsupportedOperationException();
//这里的remove()方法是无法实现的,所以只能抛出一个异常,属于是适配器的缺点了
}
}
IteratorEnumeration
package Chapter7_AdapterAndFacadePattern.Adapter.Example2;
import java.util.Enumeration;
import java.util.Iterator;
/**
* @Author 竹心
* @Date 2023/8/7
**/
public class IteratorEnumeration implements Enumeration<Object> {
public Iterator<?> iterator;
public IteratorEnumeration(Iterator<?> iterator){
this.iterator = iterator;
}
@Override
public boolean hasMoreElements() {
return this.iterator.hasNext();
}
@Override
public Object nextElement() {
return this.iterator.next();
}
}
notes.txt
适配器模式:
将一个类的接口转换成客户期望的另外一个接口。适配器让原本接口不兼容的类可以合作
对象和类适配器:
前者主要使用组合,后者使用继承(JAVA中不能使用多重继承,所以不能实现类适配器)