首页 > 代码库 > 适配器模式

适配器模式

Taget

 1 package com.learning.designpattern.adapter; 2  3 public class Target { 4      5     public void operation(){ 6         System.out.println("target operation"); 7     } 8      9 10 }

Adaptee

1 package com.learning.designpattern.adapter;2 3 public class Adaptee {4 5     public void request(){6         System.out.println("real request");7     }8 }

Adapter

 1 package com.learning.designpattern.adapter; 2  3 public class Adapter extends Target { 4      5     private Adaptee adaptee; 6      7     @Override 8     public void operation() { 9         if(adaptee==null){10             adaptee = new Adaptee();11         }12         adaptee.request();13 14     }15 16 17 }

Client:

 1 package com.learning.designpattern.adapter; 2  3 public class Client { 4  5     /** 6      * @param args 7      */ 8     public static void main(String[] args) { 9         Target target = new Adapter();10         target.operation();11 12     }13 14 }

 

适配器模式