Fun On 3 Design Patterns: Facade pattern (Facade Pattern) Fun on the design pattern of 2: Agent mode (Proxy Pattern)

  In the previous article Fun on 2 Design Patterns: Agent mode (Proxy Pattern) , we know that the agency model is essentially the process of access to the class to do the same type of control.

  There are mentioned, the API also into different modules proxy class, and these API to do the same pre-processing, it can reduce the future workload. This process also involves a design pattern - Exterior mode.

  Description of conveyance runoob:

  Intent: to provide a set of interfaces subsystem of a consistent interface schema defines the appearance of a high-level interface that makes the subsystem easier to use.

  The main solution: reduce the complexity when accessing internal subsystems of complex systems, simplifying the interface with the client.

  When to use:  1, the client does not need to know complex links within the system, the system only provides a "receptionist" can be. 2, the inlet defines the system.

  How to solve: the client is not coupled to the system, like the appearance coupled to the system.

  The key code is: between the client and add a layer of complex systems, this layer will call the order, dependencies and other deal.

  Aspect Mode is very reflected in the Spring Java web system. Spring classes using maven sort out package dependencies and calling sequence, also hides a rather complex process such as dependency injection, developers simply call filter (interceptor), written MVC three-tier structure, no complicated initialization process. This is the appearance of the model.

  Huawei cloud-based API call here, but also the process and the particular agent to encapsulate initialization process, only the outer class get VPCUtil perform operations, or packaged business function itself.

  On one, we put the virtual private cloud API class encapsulates it again, if the API class for each business to be packaged, then the implementation process is as follows:  

1. FIG TokenProxy agent followed by version 0.2, the cloud API module operation flowchart Huawei

  In fact, it can also be re-optimization, the API 4 Zhong business package together, because token use these types of services is the same, both from Huawei cloud identity verification service get ( when to use ).

  In order to reduce the amount of code the proxy class, we declare a SuperUtil class, then these four kinds of business API classes inherit (as a subclass of the key code ):

1 public abstract class SuperUtil {
2     protected String username;
3     protected String password;
4     protected String token;
5     protected String projectId;
6     protected long overDate;
7 }
Business API parent

  These attributes, business class do not need to re-declare.

 1 package zhyx_cloud;
 2 
 3 import java.lang.reflect.Method;
 4 
 5 import org.springframework.cglib.proxy.Enhancer;
 6 import org.springframework.cglib.proxy.MethodInterceptor;
 7 import org.springframework.cglib.proxy.MethodProxy;
 8 
 9 public class TokenProxy {
10     
11     private VPCUtil VPCProxy = null;
12     
13     private ECSUtil ECSProxy = null;
14     
15     private SecurityGroupUtil SecurityGroupProxy = null;
16     
17     private PublicIPUtil PublicIPProxy = null;
18     
19     private MethodInterceptor interceptorFactory(String username, String password){
20         return new MethodInterceptor() {
21             @Override
22             public Object intercept(Object sub, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
23                 SuperUtil superUtil = (SuperUtil)sub;
24                 if(superUtil.token == null || System.currentTimeMillis() > superUtil.overDate) {
25                     TokenAndProject tap = new IAMUtil().getToken(username,password);
26                     VPCUtil myproxy = (VPCUtil)sub;
27                     myproxy.token = tap.getToken();
28                     myproxy.projectId = tap.getProjectId();
29                     myproxy.overDate = System.currentTimeMillis() + 23*60*60*1000;
30                 }
31                 Object res = methodProxy.invokeSuper(sub, objects);
32                 return res;
33             }
34         };
35     }
36     
37     public TokenProxy(String username, String password) {
38         Enhancer enhancer = new Enhancer();
39         enhancer.setSuperclass(VPCUtil.class);
40         enhancer.setCallback(this.interceptorFactory(username, password));
41         this.VPCProxy= (VPCUtil)enhancer.create();
42         enhancer = new Enhancer();
43         enhancer.setSuperclass(ECSUtil.class);
44         enhancer.setCallback(this.interceptorFactory(username, password));
45         this.ECSProxy = (ECSUtil)enhancer.create();
46         enhancer = new Enhancer();
47         enhancer.setSuperclass(SecurityGroupUtil.class);
48         enhancer.setCallback(this.interceptorFactory(username, password));
49         this.SecurityGroupProxy = (SecurityGroupUtil)enhancer.create();
50         enhancer = new Enhancer();
51         enhancer.setSuperclass(PublicIPUtil.class);
52         enhancer.setCallback(this.interceptorFactory(username, password));
53         this.PublicIPProxy = (PublicIPUtil)enhancer.create();
54     }
55     
56     public VPCUtil getVPCProxy() {
57         return this.VPCProxy;
58     }
59 
60     public ECSUtil getECSProxy() {
61         return ECSProxy;
62     }
63 
64     public SecurityGroupUtil getSecurityGroupProxy() {
65         return SecurityGroupProxy;
66     }
67 
68     public PublicIPUtil getPublicIPProxy() {
69         return PublicIPProxy;
70     }
71 
72 }
TokenProxy v0.3

  The work ( key codes later) are processed, the external class calls the class, this class can be got by only four service function modules, and that these functions need to repeat the verification; and, if you want to add different types of subsequent the API business operations in the proxy class directly above the handle on it ( the main solution ).

 

 2. FIG TokenProxy agent followed by version 0.3, the cloud API module operation flowchart Huawei

  Advantages:  1, the system reduces the interdependence. 2, improve flexibility. 3, to improve security.

  Disadvantages: does not comply with the principle of opening and closing, if you want to change things too much trouble, inheritance rewrite are inappropriate. (If there are other types of business API, the proxy class initialization procedure code will be more complicated)

  Usage scenarios:  1, the module provides access to the outside world as complex modules or subsystems. 2, relatively independent subsystems. 3, bring prevent low risk personnel.

  Note: In a hierarchical structure, can define the System entry facade pattern each layer.

Guess you like

Origin www.cnblogs.com/dgutfly/p/11615085.html