设计模式学习笔记(三)之静(动)态代理模式、适配器模式

一、代理模式(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 }

 

posted @ 2016-06-03 19:43  谈笑风生~  阅读(281)  评论(0编辑  收藏  举报