"Zen design pattern" of the proxy mode

Define a proxy mode

Providing a proxy to control access to the object to other objects.

Proxy mode is also called entrusted mode, it is a basic design skills. Many other models, such as the state model, strategy pattern, the visitor pattern is essentially a more special occasions using the delegate model, but also in everyday applications, proxy mode can provide very good access control. Struts2 as a Form element mapping is to use a proxy mode.

Proxy mode generic class:

1.Subject abstract thematic roles

Abstract theme class is an abstract class can also be an interface, is one of the most common type of business definition, no special requirements.

2.RealSubject specific thematic roles

Also known as delegated role, being an agent role. It is the business logic specific performer.

3.Proxy agency thematic roles

Also known as the delegate class, proxy class. It is responsible for the application of real character, all the methods abstract class defines the theme of limiting the role entrusted achieve a real theme, and before and after treatment is completed preprocessing and rehabilitation work in the real theme of the role.

Application Second, proxy mode

Advantages of the proxy mode

(1) clear responsibilities

Real role is to implement the actual business logic, do not care about other non-transaction of this responsibility, to complete a transaction through the latter part of the agent, with the result that the program simple and clear.

(2) high scalability

Specific theme is the role will change at any time, as long as it implements the interface, no matter how it changes, are able to escape the interface, the proxy class that we can fully use without doing any modifications.

(3) intelligent

Dynamic proxy can be reflected

2. Proxy mode usage scenarios

Combined with the reality of life to a lawsuit, for example, why the court should get a lawyer? Because you do not want to participate in the middle of right and wrong, of course, as long as the completion of their defense to become, others such as feasibility study, after tracing a lawyer to get by, which is to lighten your load. SpringAop, is a very typical dynamic proxy.

Third, the best practice

Proxy mode applications is very wide, as large as a framework, enterprise platform, small code snippets, transaction processing, a little attention to use proxy mode.

Note:
When learning AOP framework, to clarify a few terms you to: section (Aspect), the entry point (JoinPoint), notification (Advice), weaving (Weave) sufficient understanding of these terms will help to better learning and understanding of its principles.

github Sample Code: https://github.com/developers-youcong/DesignPatternPractice/tree/master/Proxy

This chapter needs to be supplemented general agents, agents forced, dynamic proxy ......

Guess you like

Origin www.cnblogs.com/youcong/p/12115977.html