标签:判断 目标接口 instance 子类 接口 关联 ada 节点 stat
对象适配器
1 public class Adapter extends Adaptee implements Target 2 { 3 public void request() 4 { 5 specificRequest(); 6 } 7 }
典型的对象适配器代码:
1 public class Adapter extends Target 2 { 3 private Adaptee adaptee; 4 5 public Adapter(Adaptee adaptee) 6 { 7 this.adaptee=adaptee; 8 } 9 10 public void request() 11 { 12 adaptee.specificRequest(); 13 } 14 }
实例代码(JAVA):
1 public interface Robot 2 { 3 public void cry(); 4 public void move(); 5 } 6 7 public class Bird 8 { 9 public void tweedle() 10 { 11 System.out.println("鸟儿叽叽叫!"); 12 } 13 14 public void fly() 15 { 16 System.out.println("鸟儿快快飞!"); 17 } 18 } 19 20 public class BirdAdapter extends Bird implements Robot 21 { 22 public void cry() 23 { 24 System.out.print("机器人模仿:"); 25 super.tweedle(); 26 } 27 28 public void move() 29 { 30 System.out.print("机器人模仿:"); 31 super.fly(); 32 } 33 } 34 35 public class Dog 36 { 37 public void wang() 38 { 39 System.out.println("狗汪汪叫!"); 40 } 41 42 public void run() 43 { 44 System.out.println("狗快快跑!"); 45 } 46 } 47 48 public class DogAdapter extends Dog implements Robot 49 { 50 public void cry() 51 { 52 System.out.print("机器人模仿:"); 53 super.wang(); 54 } 55 56 public void move() 57 { 58 System.out.print("机器人模仿:"); 59 super.run(); 60 } 61 } 62 63 //配置文件config.xml 64 <?xml version="1.0"?> 65 <config> 66 <className>BirdAdapter</className> 67 </config> 68 69 //通过反射得到具体的适配器类 70 import javax.xml.parsers.*; 71 import org.w3c.dom.*; 72 import org.xml.sax.SAXException; 73 import java.io.*; 74 public class XMLUtil 75 { 76 //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 77 public static Object getBean() 78 { 79 try 80 { 81 //创建文档对象 82 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 83 DocumentBuilder builder = dFactory.newDocumentBuilder(); 84 Document doc; 85 doc = builder.parse(new File("config.xml")); 86 87 //获取包含类名的文本节点 88 NodeList nl = doc.getElementsByTagName("className"); 89 Node classNode=nl.item(0).getFirstChild(); 90 String cName=classNode.getNodeValue(); 91 92 //通过类名生成实例对象并将其返回 93 Class c=Class.forName(cName); 94 Object obj=c.newInstance(); 95 return obj; 96 } 97 catch(Exception e) 98 { 99 e.printStackTrace(); 100 return null; 101 } 102 } 103 } 104 105 //客户端 106 public class Client 107 { 108 public static void main(String args[]) 109 { 110 Robot robot=(Robot)XMLUtil.getBean(); 111 robot.cry(); 112 robot.move(); 113 } 114 }
实例代码(JAVA):
1 //目标抽象类 2 public abstract class DataOperation 3 { 4 private String password; 5 6 public void setPassword(String password) 7 { 8 this.password=password; 9 } 10 11 public String getPassword() 12 { 13 return this.password; 14 } 15 16 public abstract String doEncrypt(int key,String ps); 17 } 18 19 //适配者类 20 public final class Caesar 21 { 22 public String doEncrypt(int key,String ps) 23 { 24 String es=""; 25 for(int i=0;i<ps.length();i++) 26 { 27 char c=ps.charAt(i); 28 if(c>=‘a‘&&c<=‘z‘) 29 { 30 c+=key%26; 31 if(c>‘z‘) c-=26; 32 if(c<‘a‘) c+=26; 33 } 34 if(c>=‘A‘&&c<=‘Z‘) 35 { 36 c+=key%26; 37 if(c>‘Z‘) c-=26; 38 if(c<‘A‘) c+=26; 39 } 40 es+=c; 41 } 42 return es; 43 } 44 } 45 46 //适配器类 47 public class CipherAdapter extends DataOperation 48 { 49 private Caesar cipher; 50 51 public CipherAdapter() 52 { 53 cipher=new Caesar(); 54 } 55 56 public String doEncrypt(int key,String ps) 57 { 58 return cipher.doEncrypt(key,ps); 59 } 60 } 61 62 //新适配者类 63 public final class NewCipher 64 { 65 public String doEncrypt(int key,String ps) 66 { 67 String es=""; 68 for(int i=0;i<ps.length();i++) 69 { 70 String c=String.valueOf(ps.charAt(i)%key); 71 es+=c; 72 } 73 return es; 74 } 75 } 76 77 //新适配器类 78 public class NewCipherAdapter extends DataOperation 79 { 80 private NewCipher cipher; 81 82 public NewCipherAdapter() 83 { 84 cipher=new NewCipher(); 85 } 86 87 public String doEncrypt(int key,String ps) 88 { 89 return cipher.doEncrypt(key,ps); 90 } 91 } 92 93 //配置文件 config.xml 94 <?xml version="1.0"?> 95 <config> 96 <className>NewCipherAdapter</className> 97 </config> 98 99 //使用JAVA反射得到配置文件中的适配器类实例 100 import javax.xml.parsers.*; 101 import org.w3c.dom.*; 102 import org.xml.sax.SAXException; 103 import java.io.*; 104 public class XMLUtil 105 { 106 //该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象 107 public static Object getBean() 108 { 109 try 110 { 111 //创建文档对象 112 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 113 DocumentBuilder builder = dFactory.newDocumentBuilder(); 114 Document doc; 115 doc = builder.parse(new File("config.xml")); 116 117 //获取包含类名的文本节点 118 NodeList nl = doc.getElementsByTagName("className"); 119 Node classNode=nl.item(0).getFirstChild(); 120 String cName=classNode.getNodeValue(); 121 122 //通过类名生成实例对象并将其返回 123 Class c=Class.forName(cName); 124 Object obj=c.newInstance(); 125 return obj; 126 } 127 catch(Exception e) 128 { 129 e.printStackTrace(); 130 return null; 131 } 132 } 133 } 134 135 //客户端 136 public class Client 137 { 138 public static void main(String args[]) 139 { 140 DataOperation dao=(DataOperation)XMLUtil.getBean(); 141 dao.setPassword("sunnyLiu"); 142 String ps=dao.getPassword(); 143 String es=dao.doEncrypt(6,ps); 144 System.out.println("明文为:" + ps); 145 System.out.println("密文为:" + es); 146 } 147 }
1 public interface AdvisorAdapter{ 2 //将一个Advisor适配成MethodInterceptor 3 MethodInterceptor getInterceptor(Advisor advisor); 4 //判断此适配器是否支持特定的Advice 5 boolean supportsAdvice(Advice advice); 6 }
(3)在JDK类库中也定义了一系列适配器类,如在com.sun.imageio.plugins.common包中定义的InputStreamAdapter类,用于包装ImageInputStream接口及其子类对象。
1 public class InputStreamAdapter extends InputStream { 2 ImageInputStream stream; 3 public InputStreamAdapter(ImageInputStream stream) { 4 super(); 5 this.stream = stream; 6 } 7 public int read() throws IOException { 8 return stream.read(); 9 } 10 public int read(byte b[], int off, int len) throws IOException { 11 return stream.read(b, off, len); 12 } 13 }
标签:判断 目标接口 instance 子类 接口 关联 ada 节点 stat
原文地址:https://www.cnblogs.com/WindSun/p/10259340.html