java-Proxy Mode

Source: Internet
Author: User

What is proxy mode?

  The proxy mode provides a proxy object for an object, and the proxy object controls the reference to the original object . For example, one person or one institution acts on behalf of another person or another body. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary before the client and the target object. In fact, the mode of the agent is two words: intermediary .

Some application scenarios for proxy mode:

Railway station through the Ox ticket, through the 4S shop to buy a car, through the intermediary rent, takeout small brother delivery, etc. are the specific application of the agent model.

Proxy Mode classification:

The proxy mode can be broadly divided into two categories, static agents and dynamic proxies. Proxies are mainly implemented by reflection technology.

Role of proxy mode:

Isubject: Abstract Theme role, is an interface. This interface is the interface that the object and its agents share.
Realsubject: A real-world theme, a class that implements an abstract topic interface.
Proxy: An agent role that internally contains a reference to the real object realsubject, allowing you to manipulate the real object. The proxy object provides the same interface as the real object, so that it can replace the real object at any moment. At the same time, the proxy object can attach other actions when performing the real object operation, which is equivalent to encapsulating the real object.

Static proxy

Static proxy refers to the proxy class that we write manually, compiled before the program runs, rather than the program dynamically generated proxy class, this is called static;

Code implementation: We use the delivery of small brother meal this scenario to simulate the implementation of static proxy.

1. First create an interface to pick up the meal

/**@author @since * * Public interface Takeaway {    /**     * Take takeaway * /     void  Gettakeaway ();}

2. Declare a guest to take the meal, implement this interface

/*** User * *@author: Pc-user *@since: 2017/11/4 8:52*/ Public classMytakeawayImplementsTakeaway {PrivateString name;//name of the person who took the meal     PublicMytakeaway (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override Public voidGettakeaway () {System.out.println (name+ "Pick up meal complete"); }}

3. Declaration of a takeaway brother agent, also must be to achieve the feeding interface

/*** Takeaway Brother * *@author: Pc-user *@since: 2017/11/4 8:54*/ Public classDeliverymanproxyImplementsTakeaway {PrivateMytakeaway Mytakeaway;//object of the person who took the meal     Publicdeliverymanproxy (Mytakeaway mytakeaway) { This. Mytakeaway =Mytakeaway; } @Override Public voidGettakeaway () {System.out.print ("Takeout Little brother help");    Mytakeaway.gettakeaway (); }}

4. Client Calls

/*** TODO * *@author: Pc-user *@since: 2017/11/4 8:56*/ Public classMain { Public Static voidMain (string[] args) {Mytakeaway renter1=NewMytakeaway ("Zhang San"); Mytakeaway Renter2=NewMytakeaway ("John Doe"); Deliverymanproxy Proxy=NewDeliverymanproxy (Renter1);        Proxy.gettakeaway (); Proxy=NewDeliverymanproxy (RENTER2);    Proxy.gettakeaway (); }}

Output Result:

Takeout small brother help Zhang San take meal finish takeout Brother help John Doe take meal finish

Maybe we have questions, we can pick up our own meals? Of course this is possible, if the implementation class can meet our needs, we can directly use the implementation class. However, in the application scenario, when we want to expand the requirements, according to the design pattern of open and close principle, not recommended to directly modify the implementation class, this time we can modify the proxy class to achieve. For example, one day, we want to pick up the meal when we go downstairs to buy a pack of cigarettes, our implementation class is not to buy cigarettes this interface, this time we can modify the proxy class to achieve. In this case we can directly modify the proxy class to implement, without moving the implementation of the interface of the class;

/*** Takeaway Brother * *@author: Pc-user *@since: 2017/11/4 8:54*/ Public classDeliverymanproxyImplementsTakeaway {PrivateMytakeaway Mytakeaway;//object of the person who took the meal     Publicdeliverymanproxy (Mytakeaway mytakeaway) { This. Mytakeaway =Mytakeaway; } @Override Public voidGettakeaway () {System.out.print ("Takeout Little brother help");        Mytakeaway.gettakeaway (); System.out.print ("Comfortable and comfortable");    Buycigarettes (); }    Private voidbuycigarettes () {System.out.println ("Bought a pack of cigarettes."); }}

Test class unchanged, run result:

Takeout small brother help Zhang San take meal complete and handy bought a pack of cigarettes ======================== takeout small brother help John Doe take meal complete and handy bought a pack of cigarettes

Dynamic Agent

java-Proxy Mode

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.