head first-----------------深入浅出适配器模式
适配器模式:将一个类的接口,转换成客户想要的另外一个接口,适配器然原本接口不兼容的类可以合作无间。从而可以不用更改旧的代码就可以适应新的业务需求
package com.clark.adapterpattern.abstractclass;
/**
* Duck 接口
* @author Administrator
*
*/
public interface Duck {
public void quack();//呱呱叫方法
public void fly();//鸭子会飞
}
/**
* Duck 接口
* @author Administrator
*
*/
public interface Duck {
public void quack();//呱呱叫方法
public void fly();//鸭子会飞
}
package com.clark.adapterpattern.abstractclass;
/**
* 火鸡接口
* @author Administrator
*
*/
public interface Turkey {
public void gobble();//火鸡嘎嘎叫
public void fly();//火鸡也会飞,只是飞得短
}
/**
* 火鸡接口
* @author Administrator
*
*/
public interface Turkey {
public void gobble();//火鸡嘎嘎叫
public void fly();//火鸡也会飞,只是飞得短
}
package com.clark.adapterpattern;
import com.clark.adapterpattern.abstractclass.Duck;
/**
* 绿头鸭
* @author Administrator
*
*/
public class MallardDuck implements Duck {
@Override
public void quack() {
System.out.println("Quack.......");
}
@Override
public void fly() {
System.out.println("fly remote distance.......");
}
}
import com.clark.adapterpattern.abstractclass.Duck;
/**
* 绿头鸭
* @author Administrator
*
*/
public class MallardDuck implements Duck {
@Override
public void quack() {
System.out.println("Quack.......");
}
@Override
public void fly() {
System.out.println("fly remote distance.......");
}
}
package com.clark.adapterpattern;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 火鸡的具体实现
* @author Administrator
*
*/
public class WildTurkey implements Turkey {
@Override
public void gobble() {
System.out.println("Gobble gobble.....");
}
@Override
public void fly() {
System.out.println("fly short distance.....");
}
}
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 火鸡的具体实现
* @author Administrator
*
*/
public class WildTurkey implements Turkey {
@Override
public void gobble() {
System.out.println("Gobble gobble.....");
}
@Override
public void fly() {
System.out.println("fly short distance.....");
}
}
package com.clark.adapterpattern;
import java.util.Random;
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 鸭子转换成火鸡的适配器
* @author Administrator
*
*/
public class DuckAdpter implements Turkey {
public Duck duck;
public Random rand;
public DuckAdpter(Duck duck){
this.duck=duck;
rand=new Random();
}
@Override
public void gobble() {
this.duck.quack();
}
@Override
public void fly() {
if(rand.nextInt(5)==0){
this.duck.fly();
}
}
}
import java.util.Random;
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 鸭子转换成火鸡的适配器
* @author Administrator
*
*/
public class DuckAdpter implements Turkey {
public Duck duck;
public Random rand;
public DuckAdpter(Duck duck){
this.duck=duck;
rand=new Random();
}
@Override
public void gobble() {
this.duck.quack();
}
@Override
public void fly() {
if(rand.nextInt(5)==0){
this.duck.fly();
}
}
}
package com.clark.adapterpattern;
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 火鸡转化成鸭子的适配器
* @author Administrator
*
*/
public class TurkeyAdpter implements Duck {
public Turkey turkey;
public TurkeyAdpter(Turkey turkey){
this.turkey=turkey;
}
@Override
public void quack() {
this.turkey.gobble();
}
@Override
public void fly() {
//由于火鸡飞得距离短,所以让它多飞几次
for (int i = 0; i < 3; i++) {
this.turkey.fly();
}
}
}
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* 火鸡转化成鸭子的适配器
* @author Administrator
*
*/
public class TurkeyAdpter implements Duck {
public Turkey turkey;
public TurkeyAdpter(Turkey turkey){
this.turkey=turkey;
}
@Override
public void quack() {
this.turkey.gobble();
}
@Override
public void fly() {
//由于火鸡飞得距离短,所以让它多飞几次
for (int i = 0; i < 3; i++) {
this.turkey.fly();
}
}
}
package com.clark.adapterpattern;
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* Test CLASS
* @author Administrator
*
*/
public class TestDrive {
public static void main(String[] args) {
//先创建一只鸭子和一只火鸡
MallardDuck duck=new MallardDuck();
WildTurkey turkey=new WildTurkey();
//然后将火鸡包装进火鸡适配器,鸭子装到鸭子适配器
Duck turkryAdpter=new TurkeyAdpter(turkey);
Turkey DuckAdpter=new DuckAdpter(duck);
//打印原来火鸡和鸭子的行为
System.out.println("The Turkey says.....");
turkey.gobble();
turkey.fly();
System.out.println(" The Duck says.....");
testDuck(duck);
//使用适配器之后输出的行为
System.out.println(" The turkeyadpter says....");
testDuck(turkryAdpter);
System.out.println(" The turkeyAdpter says....");
testTurkey(turkey);
}
public static void testDuck(Duck duck){
duck.quack();
duck.fly();
}
public static void testTurkey(Turkey turkey){
turkey.gobble();
turkey.fly();
}
}
import com.clark.adapterpattern.abstractclass.Duck;
import com.clark.adapterpattern.abstractclass.Turkey;
/**
* Test CLASS
* @author Administrator
*
*/
public class TestDrive {
public static void main(String[] args) {
//先创建一只鸭子和一只火鸡
MallardDuck duck=new MallardDuck();
WildTurkey turkey=new WildTurkey();
//然后将火鸡包装进火鸡适配器,鸭子装到鸭子适配器
Duck turkryAdpter=new TurkeyAdpter(turkey);
Turkey DuckAdpter=new DuckAdpter(duck);
//打印原来火鸡和鸭子的行为
System.out.println("The Turkey says.....");
turkey.gobble();
turkey.fly();
System.out.println(" The Duck says.....");
testDuck(duck);
//使用适配器之后输出的行为
System.out.println(" The turkeyadpter says....");
testDuck(turkryAdpter);
System.out.println(" The turkeyAdpter says....");
testTurkey(turkey);
}
public static void testDuck(Duck duck){
duck.quack();
duck.fly();
}
public static void testTurkey(Turkey turkey){
turkey.gobble();
turkey.fly();
}
}
//=============Test result================
The Turkey says.....
Gobble gobble.....
fly short distance.....
The Duck says.....
Quack.......
fly remote distance.......
The turkeyadpter says....
Gobble gobble.....
fly short distance.....
fly short distance.....
fly short distance.....
The turkeyAdpter says....
Gobble gobble.....
fly short distance.....
Gobble gobble.....
fly short distance.....
The Duck says.....
Quack.......
fly remote distance.......
The turkeyadpter says....
Gobble gobble.....
fly short distance.....
fly short distance.....
fly short distance.....
The turkeyAdpter says....
Gobble gobble.....
fly short distance.....