Proxy mode in Design Mode

Source: Internet
Author: User
I. What is proxy mode?

Definition of proxy mode:Provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or cannot directly reference another object, and the proxy object can be used between the client and the target object.Intermediary. In general.It means that the real business functions are implemented by the delegate class, but some public services before the business is implemented, for example, some functions such as buffering and logging are missing in the project development. You can use a proxy to enable the encapsulated delegate class.

Proxy mode in real life: The landlord --> intermediary --> the tenant intermediary is the agent, responsible for house viewing, talking about price, signing for equivalent things, the landlord only needs to collect money.

Ii. Why is there a proxy mode? (1 ). clear responsibilities: the real role is to implement the actual business logic, do not care about other transactions that are not in this responsibility, complete a transaction through a later agent, with the result that the programming is concise and clear. (2 ). the proxy object can act as an intermediary between the client and the target object. The client uses methods in the proxy object, which plays the role of the intermediary and protects the target object. (3 ). high scalability: No matter how the proxy object changes, as long as the proxy class and the proxy class implement a unified interface, the proxy class can be modified differently. Even if a new proxy class is extended, the proxy class can also be used, you only need to input the corresponding proxy class object when creating the proxy class. Iii. How to Use the proxy mode? 3.1 static proxy

Static proxy definition: a static proxy is created by a programmer or automatically generated by a specific tool before being compiled.The biggest feature is that the. Class file of the proxy class already exists when the program is running.But this has a major defect:Each proxy class can only be an interface service.

3.1.1 static proxy example:

①: Define service interfaces

package com.canner.proxy;/** * @Auther: canner * @Date: 12:18 2018/11/02  */public interface ICustomer {    void buyHosue();}

②: Define the service implementation class

Import COM. canner. proxy. icustomer;/*** @ auther: Canner * @ Date: 12:20 2018/11/02 */public class customerimpl implements icustomer {@ override public void buyhosue () {system. out. println ("customer bought a suite ");}}

③: Create a customer agent

Package COM. canner. proxy; import COM. canner. proxy. icustomer;/*** @ auther: Canner * @ Date: 12:33 2018/11/02 */public class customerproxy implements icustomer {private icustomer customer; Public customerproxy (final icustomer customer) {This. customer = Customer ;}@ override public void buyhosue () {system. out. println ("what the customer did before buying a house"); customer. buyhosue (); system. out. println ("what the customer did after buying a house ");}}

④: Write a test

/** * @Auther: canner * @Date: 12:25 2018/11/02 */public class ProxyTest {    public static void main(String[] args) {        ICustomer customer = new CustomerImpl();        CustomerProxy customerProxy = new CustomerProxy(customer);        customerProxy.buyHosue();    }}

Static proxy summary:The proxy object and the actual object implement the same interface

Advantage: the function of the target object can be expanded in compliance with the open and closed principles.

Disadvantages: 1:Each proxy class can only be an interface serviceIt is too heavy to manage. Once the interface changes, the proxy class must be modified accordingly. 

2: The. Class file of the static proxy already exists before the program runs.

3: if a new method is added to the interface, the proxy class must also maintain the new content.

3.2 dynamic proxy 3.2.1 JDK dynamic proxy

Dynamic Proxy: In the dynamic proxy, we no longer need to manually create the proxy class. We only need to write a dynamic processor. The real proxy object is dynamically created by JDK using reflection during runtime.

JDK dynamic proxy API analysis :(The real object must have interfaces.)

1. java. Lang. Reflect. proxy class: the parent class of all dynamic proxy classes generated by the Java Dynamic proxy mechanism. It provides a set of static methods.

To dynamically generate the proxy class and its objects for a group of interfaces.

Main Methods:

Public static object newproxyinstance (classloader loader, class <?> [] Interfaces, invocationhandler hanlder)

Method responsibilities: generate a dynamic proxy instance for a specified class loader, a group of interfaces, and a call Processor

Parameters:

Loader: A class loader that generally transmits real objects.

Interfaces: interface to be implemented by the proxy class

Hanlder: How to Enhance proxy objects:

Return: The created proxy object.

2. java. Lang. Reflect. invocationhandler interface:

Public object invoke (Object proxy, method, object [] ARGs)

Method responsibility: handles all method calls in the dynamic proxy class in a centralized manner.

Parameters:

Proxy: The generated proxy object.

Method: The actual method object currently called.

ARGs: real parameters of the currently called Method

Return: returns the result of a real method.

Proxy mode in Design 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.