Java proxy Mode

Source: Internet
Author: User

1. Introduction Proxy Pattern is one of the 23 common Java Design Patterns in GoF. Definition of proxy mode: Provide a surrogate or placeholder for another object to controlaccess to it (provides a proxy for other objects to control access to this object ). Create a proxy object in proxy mode to allow the proxy object to control access to the target object (the target object can be a remote object, an overhead object can be created, or an object requiring security control ), in addition, some additional functions can be added without changing the target object. 2. UML class diagram 3. role and Subject in the mode: Abstract topic role. The abstract topic class can be an abstract class or an interface. It is the most common business type definition and has no special requirements. RealSubject: a topic role, also called a delegated role or a delegated role. Is the specific executor of the business logic. Proxy: the Proxy topic role, also called the delegate class and Proxy class. It implements all the methods for defining abstract themes for specific topic roles, and performs preprocessing and aftercare before and after the specific topic roles are processed. (The simplest one is printing logs.) 4. code implementation (1) Subjectpackage com. jackie. designpatterns. proxy;/*** abstract topic, define main functions */publicinterface Subject {publicvoid operate ();} (2) RealSubjectpackage com. jackie. designpatterns. proxy;/*** Subject */publicclass RealSubject implements Subject {@ Override publicvoid operate () {System. out. println ("realsubject operatestarted ...... ") ;}} (3) Proxypackage com. jackie. designpatterns. proxy;/*** proxy class * /Publicclass Proxy implements Subject {private Subject subject; public Proxy (Subject subject) {this. subject = subject;} @ Override publicvoid operate () {System. out. println ("before operate ...... "); subject. operate (); System. out. println ("after operate ...... ") ;}} (4) Clientpackage com. jackie. designpatterns. proxy;/*** Client */publicclass Client {/*** @ param args */publicstaticvoid main (String [] arg S) {Subject subject = new RealSubject (); Proxy proxy = new Proxy (subject); proxy. operate () ;}} running result: beforeoperate ...... realsubject operate started ...... afteroperate ...... 5. in the real world, the secretary is equivalent to an agent and the boss has a meeting. In this case, the secretary can hand over the work of notifying employees of the meeting time, arranging the meeting venue, organizing the meeting venue after the meeting, and so on, the boss only needs a meeting and does not need to do those things in person. Similarly, in our program design, we can also use the proxy mode to decouple the code that is composed of a series of irrelevant logic. For example, the log code in the Business Code can be carried out in the proxy. Spring AOP is a typical dynamic proxy application. 6. Proxy Mode Application Form (1) Remote Proxy-the fact that an object exists in a different address space can be hidden. The client can also access objects on a remote machine. The remote machine may have better computing performance and processing speed, and can quickly respond to and process client requests. (2) Virtual Proxy-allows objects with high memory overhead to be created as needed. This object is created only when it is actually needed. (3) Copy-On-Write Proxy (Copy-On-Write Proxy)-used to control object replication by delaying object replication until the customer really needs it. Is a variant of the virtual proxy. (4) Protection Proxy-provide different levels of target object Access permissions for different customers (5) Cache Proxy) -provides temporary storage for overhead computing results. It allows multiple customers to share results to reduce computing or network latency. (6) Firewall Proxy-controls access to network resources and protects the subject from malicious customer attacks. (7) SynchronizationProxy-Provides secure access to the topic in the case of multiple threads. (8) Smart ReferenceProxy-provides some additional operations when an object is referenced, such as recording the number of calls to this object. (9) Complexity HidingProxy: used to hide the Complexity of a complex set of classes and perform access control. It is sometimes called the appearance Proxy, which is not hard to understand. The complex hidden proxy and appearance mode are different because the proxy controls access, while the appearance mode is different because the proxy controls access, while the appearance mode only provides another set of interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.