适配器模式(adapter pattern)(插座转换)

 

定义:将一个类的借口,转换成客户期望的另一个借口。适配器让原本借口不兼容的泪可以合作无间。

笔记:转换器。

 

 

鸭子接口

package com.tj.adapter.duck; public interface Duck { /** * duck quack interface method */ public void quack(); /** * duck fly interface method */ public void fly(); }  

 

鸭子实现类

package com.tj.adapter.duck; public class MallardDuck implements Duck { /** * @see com.tj.adapter.duck.Duck#quack() */ @Override public void quack() { System.out.println("Quack"); } /** * @see com.tj.adapter.duck.Duck#fly() */ @Override public void fly() { System.out.println("I'm flying"); } } 

 

火鸡接口

package com.tj.adapter.turkey; public interface Turkey { /** * Turkey gobble interface method */ public void gobble(); /** * Turkey fly interface method */ public void fly(); }  

 

火鸡实现类

package com.tj.adapter.turkey; public class WildTurkey implements Turkey { /** * @see com.tj.adapter.turkey.Turkey#gobble() */ @Override public void gobble() { System.out.println("Gobble gobble"); } /** * @see com.tj.adapter.turkey.Turkey#fly() */ @Override public void fly() { System.out.println("I'm flying a short distance"); } }  

 

鸭子适配器,将鸭子变火鸡

package com.tj.adapter.adapter; import java.util.Random; import com.tj.adapter.duck.Duck; import com.tj.adapter.turkey.Turkey; public class DuckAdapter implements Turkey { Duck duck; Random rand; public DuckAdapter(Duck duck) { this.duck = duck; rand = new Random(); } /** * @see com.tj.adapter.turkey.Turkey#gobble() */ @Override public void gobble() { duck.quack(); } /** * @see com.tj.adapter.turkey.Turkey#fly() */ @Override public void fly() { if (rand.nextInt(5) == 0) { duck.fly(); } } }  

 

火鸡适配器,将火鸡变鸭子

package com.tj.adapter.adapter; import com.tj.adapter.duck.Duck; import com.tj.adapter.turkey.Turkey; public class TurkeyAdapter implements Duck { Turkey turkey; public TurkeyAdapter(Turkey turkey) { this.turkey = turkey; } /** * @see com.tj.adapter.duck.Duck#quack() */ @Override public void quack() { turkey.gobble(); } /** * @see com.tj.adapter.duck.Duck#fly() */ @Override public void fly() { for (int i = 0; i < 5; i++) { turkey.fly(); } } }  

 

应用入口1,火鸡变鸭子

package com.tj.adapter; import com.tj.adapter.adapter.TurkeyAdapter; import com.tj.adapter.duck.Duck; import com.tj.adapter.duck.MallardDuck; import com.tj.adapter.turkey.WildTurkey; public class DuckTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); WildTurkey turkey = new WildTurkey(); Duck turkeyAdapter = new TurkeyAdapter(turkey); System.out.println("The Turkey says..."); turkey.gobble(); turkey.fly(); System.out.println("/nThe Duck says..."); testDuck(duck); System.out.println("/nThe TurkeyAdapter says..."); testDuck(turkeyAdapter); } static void testDuck(Duck duck) { duck.quack(); duck.fly(); } }  

 

应用入口2,鸭子变火鸡.

package com.tj.adapter; import com.tj.adapter.adapter.DuckAdapter; import com.tj.adapter.duck.MallardDuck; import com.tj.adapter.turkey.Turkey; public class TurkeyTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); Turkey duckAdapter = new DuckAdapter(duck); for (int i = 0; i < 10; i++) { System.out.println("The DuckAdapter says..."); duckAdapter.gobble(); duckAdapter.fly(); } } }  

 

你可能感兴趣的:(head,first,设计模式)