Java 设计模式-适配器模式(Adapter 转的)

  适配器模式,从名字就可以很好的理解该模式。 我们平时使用的电子产品的适配器,就是将我们的生活电压220v 转换为电子产品使用的5V电压。 原本220V电压的生活用电是没有办法作用在电子产品上的,可是我们增加了一个适配器后就可以完美的解决了这个问题。

   适配器模式(Adapter Pattern )就是把一个类的接口变换成用户期待的另一种接口,从而使原本因接口不匹配无法在一起工作的两个类可以在一起工作。

    适配器模式有类的适配器模式和对象的适配器模式两种。

Java代码    收藏代码
  1. /** 
  2.  * Apatee ,需要被适配的接口 
  3.  *  生活电压接口 
  4.  * @author zhangwei 
  5.  * @version $Id: LivingElectric.java, v 0.1 2014年9月23日 下午10:23:55 zhangwei Exp $ 
  6.  */  
  7. public interface LivingElectric {  
  8.   
  9.     /** 
  10.      * 返回生活用电的电压 
  11.      * 
  12.      * @return 
  13.      */  
  14.     public int getLivingElectric();  
  15.   
  16. }  
  17. /** 
  18.  * Target: 这个是笔记本期待的电压接口 
  19.  * 笔记本的电压接口 
  20.  * @author zhangwei 
  21.  * @version $Id: NoteElectric.java, v 0.1 2014年9月23日 下午10:26:44 zhangwei Exp $ 
  22.  */  
  23. public interface NoteElectric {  
  24.   
  25.     /** 
  26.      * 获取笔记本的电压 
  27.      * 
  28.      * @return 
  29.      */  
  30.     public int getNoteElectric();  
  31.   
  32. }  
  33. /** 
  34.  *Adapter 将生活电压转换为笔记本需要的电压 
  35.  * @author Lenovo 
  36.  * @version $Id: ElectricAdapter.java, v 0.1 2014年9月23日 下午10:29:16 Lenovo Exp $ 
  37.  */  
  38. public class ElectricAdapter implements NoteElectric {  
  39.   
  40.     private LivingElectric livingElectric;  
  41.   
  42.     /** 
  43.      * @see com.cathy.demo.pattern.adapter.NoteElectric#getNoteElectric() 
  44.      */  
  45.     public int getNoteElectric() {  
  46.   
  47.         return livingElectric.getLivingElectric() / 40;  
  48.   
  49.     }  
  50.   
  51.     public ElectricAdapter(LivingElectric livingElectric) {  
  52.         super();  
  53.         this.livingElectric = livingElectric;  
  54.     }  
  55.   
  56. }  
  57. /** 
  58.  * 笔记本类 
  59.  * @author Lenovo 
  60.  * @version $Id: Note.java, v 0.1 2014年9月23日 下午10:29:34 Lenovo Exp $ 
  61.  */  
  62. public class Note {  
  63.     NoteElectric power;  
  64.   
  65.     public void setPower(NoteElectric noteElectric) {  
  66.         power = noteElectric;  
  67.     }  
  68.   
  69.     public void use() {  
  70.         if (power.getNoteElectric() > 7 || power.getNoteElectric() <= 0) {  
  71.             throw new IllegalArgumentException("电压不匹配,无法工作");  
  72.         }  
  73.         System.out.println(MessageFormat.format("当前电压{0}V-符合工作条件开始工作", power.getNoteElectric()));  
  74.     }  
  75. }  

 

Java代码    收藏代码
  1. /** 
  2.  * 
  3.  * @author zhangwei 
  4.  * @version $Id: Client.java, v 0.1 2014年9月23日 下午10:33:13 Lenovo Exp $ 
  5.  */  
  6. public class Client {  
  7.     public static void main(String[] args) {  
  8.         Note note = new Note();  
  9.         // 连接电源  
  10.         note.setPower(new ElectricAdapter(new LivingElectric() {  
  11.   
  12.             /** 
  13.              * 返回生活电压220V 
  14.              * @see com.cathy.demo.pattern.adapter.LivingElectric#getLivingElectric() 
  15.              */  
  16.             public int getLivingElectric() {  
  17.                 return 220;  
  18.             }  
  19.         }));  
  20.         // 使用笔记本  
  21.         note.use();  
  22.     }  
  23. }  

 

      运行的结果是:当前电压5V-符合工作条件开始工作

 

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