设计模式学习笔记(三)之静(动)态代理模式、适配器模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式学习笔记(三)之静(动)态代理模式、适配器模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、代理模式(Proxy):
?
(1)靜態代理模式:
?
簡單理解:
一個人或事物代替另一個人或事物做某件事。例如:在Tom自我介紹之前,代理器自動幫他說Hello。
?
實現:
1 public interface Subject { 2 3 public void print(); 4 } 5 6 public class RealSubject implements Subject{ 7 8 public void print(){ 9 System.out.println("I am Tom"); 10 } 11 12 } 13 14 public class Proxy implements Subject { 15 private Subject subject; 16 public Proxy(Subject subject){ 17 this.subject=subject; 18 } 19 @Override 20 public void print() { 21 // TODO Auto-generated method stub 22 System.out.println("Hello"); 23 this.subject.print(); 24 } 25 } 26 27 public class Test { 28 29 public static void main(String[] args) { 30 Subject staticProxy=new RealSubject(); 31 Proxy proxy=new Proxy(staticProxy); 32 proxy.print(); 33 34 } 35 }?
?
輸出:
Hello
I am Tom
?
靜態代理模式的缺點:
靜態代理模式寫的太死,不利于維護和擴展。也就是說當我們有多個目標對象需要代理時,我就需要建立多個代理類,改變原有的代碼。
?
動態代理模式運用反射解決了這個每個代理類都要重復寫的問題。
?
(2)動態代理模式:
實現:
1 public interface IAnimal { 2 3 public void go(); 4 public void bark(); 5 } 6 7 public class Cat implements IAnimal { 8 9 @Override 10 public void go() { 11 // TODO Auto-generated method stub 12 System.out.println("going..."); 13 } 14 15 @Override 16 public void bark() { 17 // TODO Auto-generated method stub 18 System.out.println("barking..."); 19 } 20 21 } 22 23 public class DyProxy implements InvocationHandler{ 24 25 private Object obj; 26 27 public DyProxy(Object obj){ 28 this.obj=obj; 29 } 30 31 @Override 32 public Object invoke(Object proxy, Method method, Object[] args) 33 throws Throwable { 34 // TODO Auto-generated method stub 35 36 System.out.println("動態代理!"); 37 38 return method.invoke(this.obj, args); 39 } 40 41 } 42 43 public class Test { 44 45 public static void main(String[] args) { 46 47 IAnimal animal=new Cat(); 48 49 InvocationHandler handler=new DyProxy(animal); 50 51 Object o=Proxy.newProxyInstance(animal.getClass().getClassLoader(), 52 animal.getClass().getInterfaces(), handler); 53 54 ((IAnimal)o).go(); 55 ((IAnimal)o).bark(); 56 57 } 58 }?
?輸出:?
動態代理!
going...
動態代理!
barking...
?
二、適配器模式(Adapter):
簡單理解:
將一個類的接口轉換成客戶希望的另外一個接口。使得原本由于接口不兼容而不能一起工作的那些類可以在一起工作。
就像翻譯官能讓兩個不同語言的人溝通;手機充電器能降壓給手機充電。
?
(1)類的適配器模式(采用繼承實現):
適配器使本來無法兼容到一起的植物和動物產生了聯系。
1 public interface Plant { 2 public void water(); 3 } 4 5 public class Animal { 6 7 public void bark(){ 8 System.out.println("叫"); 9 } 10 11 } 12 13 public class Adapter extends Animal implements Plant{ 14 15 @Override 16 public void water() { 17 // TODO Auto-generated method stub 18 super.bark(); 19 } 20 21 } 22 23 public class Test { 24 25 public static void main(String[] args) { 26 Plant p=new Adapter(); 27 p.water(); 28 } 29 }?
?
(2)對象適配器模式(采用對象組合方式實現):?
?
1 public interface Plant { 2 3 public void water(); 4 } 5 6 public class Animal { 7 8 public void bark(){ 9 System.out.println("叫"); 10 } 11 } 12 13 public class Adapter implements Plant{ 14 private Animal animal=new Animal(); 15 @Override 16 public void water() { 17 // TODO Auto-generated method stub 18 animal.bark(); 19 } 20 21 } 22 23 public class Test { 24 25 public static void main(String[] args) { 26 Plant p=new Adapter(); 27 p.water(); 28 } 29 }?
轉載于:https://www.cnblogs.com/CZDblog/p/5556911.html
總結
以上是生活随笔為你收集整理的设计模式学习笔记(三)之静(动)态代理模式、适配器模式的全部內容,希望文章能夠幫你解決所遇到的問題。