Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6097013
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类: Java

2013-12-06 03:17:21

原文地址:设计模式之适配器模式 作者:hsh0756232

适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。
一、类的适配器模式

点击(此处)折叠或打开

  1. package cn.edu.shou.hsh;

  2. public class Source {
  3.     public void method_one(){
  4.         System.out.println("one......");
  5.     }
  6. }


  7. package cn.edu.shou.hsh;

  8. public interface Targetable {
  9.     void method_one();
  10.     void method_two();
  11. }


  12. package cn.edu.shou.hsh;

  13. /*
  14.  * 类的适配器
  15.  */
  16. public class Adapter_class extends Source implements Targetable {

  17.     @Override
  18.     public void method_two() {
  19.         // TODO Auto-generated method stub
  20.         System.out.println("two......");
  21.     }

  22. }


  23. package cn.edu.shou.hsh;

  24. public class Test_class {

  25.     /**
  26.      * @param args
  27.      */
  28.     public static void main(String[] args) {
  29.         // TODO Auto-generated method stub
  30.         Targetable target=new Adapter_class();
  31.         target.method_two();
  32.         target.method_one();
  33.         
  34.     }

  35. }

二、对象的适配器模式

点击(此处)折叠或打开

  1. package cn.edu.shou.hsh;

  2. public class Source {
  3.     public void method_one(){
  4.         System.out.println("one......");
  5.     }
  6. }


  7. package cn.edu.shou.hsh;

  8. public interface Targetable {
  9.     void method_one();
  10.     void method_two();
  11. }


  12. package cn.edu.shou.hsh;

  13. /*
  14.  * 对象适配器
  15.  */
  16. public class Adapter_object implements Targetable {

  17.     private Source source;
  18.     public Adapter_object(Source source) {
  19.         super();
  20.         // TODO Auto-generated constructor stub
  21.         this.source=source;
  22.     }

  23.     
  24.     @Override
  25.     public void method_one() {
  26.         // TODO Auto-generated method stub
  27.         source.method_one();
  28.     }

  29.     @Override
  30.     public void method_two() {
  31.         // TODO Auto-generated method stub
  32.         System.out.println("two......");
  33.     }

  34. }

  35. package cn.edu.shou.hsh;

  36. public class Test_object {

  37.     /**
  38.      * @param args
  39.      */
  40.     public static void main(String[] args) {
  41.         // TODO Auto-generated method stub
  42.         Source source = new Source();
  43.         Adapter_object ao=new Adapter_object(source);
  44.         ao.method_one();
  45.         ao.method_two();
  46.     }

  47. }
三、接口的适配器模式

点击(此处)折叠或打开

  1. package cn.edu.shou.hsh;

  2. public interface Targetable {
  3.     void method_one();
  4.     void method_two();
  5. }


  6. package cn.edu.shou.hsh;

  7. public abstract class Wrapper implements Targetable {

  8.     @Override
  9.     public void method_one() {
  10.         // TODO Auto-generated method stub

  11.     }

  12.     @Override
  13.     public void method_two() {
  14.         // TODO Auto-generated method stub

  15.     }

  16. }


  17. package cn.edu.shou.hsh;

  18. public class Interface_sub1 extends Wrapper {
  19.     public void method_one(){
  20.         System.out.println("one......");
  21.     }
  22. }


  23. package cn.edu.shou.hsh;

  24. public class Interface_sub2 extends Wrapper {
  25.     public void method_two(){
  26.         System.out.println("two......");
  27.     }
  28. }


  29. package cn.edu.shou.hsh;

  30. public class Test_interface {

  31.     /**
  32.      * @param args
  33.      */
  34.     public static void main(String[] args) {
  35.         // TODO Auto-generated method stub
  36.         Targetable target1=new Interface_sub1();
  37.         Targetable target2=new Interface_sub2();
  38.         target1.method_one();
  39.         target2.method_two();
  40.     }

  41. }

类的适配器模式:当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。

对象的适配器模式:当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。

接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

阅读(569) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~