Design Patterns static proxy mode

Static proxy mode

 

  Indirect access to the object, thereby limiting, enhance or modify some features of the object.

 

       Proxy mode to a certain object provides a proxy object, the proxy object controlled by a reference to the original object.

Static agents

  Or create a specific tool automatically generates source code by the programmer, in its compilation.

  Before programmers run, the proxy class .class file has been created.

Dynamic Proxy

  Is dynamically created by the reflection in the program is running.

Static proxy Summary:

Advantages: can be done on the target object extensions in conformity with the principle of opening and closing.

Cons: We have had to create for each service proxy class, heavy workload and difficult to manage.

   Once changed while the interface, the proxy class have to be amended accordingly.         

 1 public class ProxyDemo {
 2     public static void main(String[] args) {
 3         new proxy().myshop();
 4     }
 5 }
 6 
 7 interface a {
 8     void shopping();
 9 }
10 
11 class b implements a {
12 
13     @Override
14     public void shopping() {
15         System.out.println("买零食");
16     }
17 }
18 
19 class proxy {
20     private a a = new b();
21 
22     public void myshop() {
23         before();
24         a.shopping();
25         after();
26     }
27 
28     private void before() {
29         System.out.println("拿钱");
30     }
31 
32     private void after() {
33         System.out.println("回家啊");
34     }
35 }

 

    

Guess you like

Origin www.cnblogs.com/loveer/p/11279834.html