码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式 8 —— 适配器模式

时间:2014-10-09 20:14:37      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   使用   ar   for   sp   

设计模式目录:

设计模式 1 ——观察者模式

设计模式 2 —— 装饰者模式 

设计模式 3 —— 迭代器和组合模式(迭代器)

设计模式 4 —— 迭代器和组合模式(组合)

设计模式 5 —— 工模式

设计模式 6 —— 单件模式

设计模式 7 —— 命令模式

设计模式 8 —— 适配器模式

 

概述

第1 部分 问题引入

第2 部分 实现

第3 部分 定义

 

第1 部分 问题引入

OO适配器是什么,现实中到处都是。比方说:如果你需要在欧洲国家使用美国制造的笔记本电脑,你可能需要使用一个交流电的适配器。其实,OO适配器和真实世界的适配器扮演着同样的角色;将一个接口转换成另一个接口,以符合客户的期望。

bubuko.com,布布扣

 

第2 部分 实现

看看使用中的适配器实现:

首先是鸭子接口:

 1 /**
 2  * 鸭子接口
 3  * @ClassName: Duck
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午3:33:04
 7  */
 8 public interface Duck {
 9     
10     public void quack();
11     public void fly();
12 
13 }

 

绿头鸭,鸭子的子类

 1 /**
 2  * 绿头鸭 
 3  * @ClassName: MallardDuck
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午3:38:54
 7  */
 8 public class MallardDuck implements Duck{
 9 
10     @Override
11     public void quack() {
12         System.out.println("Quack");
13     }
14 
15     @Override
16     public void fly() {
17         System.out.println("I‘m flying.");
18     }
19 
20 }

 

火鸡:

 1 /**
 2  * 火鸡
 3  * @ClassName: Turkey
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午3:54:37
 7  */
 8 public interface Turkey {
 9     
10     public void gobble();
11     public void fly();
12 
13 }

 

 

 1 /**
 2  * 火鸡的具体实现类
 3  * @ClassName: WildTurkey
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午3:55:23
 7  */
 8 public class WildTurkey implements Turkey{
 9 
10     @Override
11     public void gobble() {
12         System.out.println("Gobble gobble.");
13     }
14 
15     @Override
16     public void fly() {
17         System.out.println("I‘m flying a short distance.");
18     }
19 
20 }

 

鸭子的适配器:

 1 /**
 2  * 鸭子适配器
 3  * @ClassName: TurkeyAdapter
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午3:58:12
 7  */
 8 public class TurkeyAdapter implements Duck{
 9     
10     Turkey turkey;
11     
12     public TurkeyAdapter(Turkey turkey){
13         this.turkey = turkey;
14     }
15 
16     @Override
17     public void quack() {
18         turkey.gobble();
19     }
20 
21     @Override
22     public void fly() {
23         for(int i=0;i<5;i++){
24             turkey.fly();
25         }
26         
27     }
28 
29 }

 

测试程序:

 1 /**
 2  * 
 3  * @ClassName: TurkeyTestDrive
 4  * TODO
 5  * @author Xingle
 6  * @date 2014-10-9 下午4:03:21
 7  */
 8 public class TurkeyTestDrive {
 9     public static void main(String[] args){
10         MallardDuck duck = new MallardDuck();
11         WildTurkey turkey = new WildTurkey();
12         
13         Duck turkeyAdapter = new TurkeyAdapter(turkey);
14         System.out.println("The Turkey says...");
15         turkey.gobble();
16         turkey.fly();
17         
18         System.out.println("\nThe Duck says...");
19         testDuck(duck);
20         
21         System.out.println("\nThe TurkeyAdapter says...");
22         testDuck(turkeyAdapter);
23     }
24 
25      static void testDuck(Duck duck) {
26          duck.quack();
27          duck.fly();
28     }
29 
30 }

 

执行结果:

bubuko.com,布布扣

 

第3 部分 定义

适配器是将

设计模式 8 —— 适配器模式

标签:style   blog   http   color   os   使用   ar   for   sp   

原文地址:http://www.cnblogs.com/xingele0917/p/4013913.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!