Home > Java > javaTutorial > The role of the proxy pattern in design patterns

The role of the proxy pattern in design patterns

零下一度
Release: 2017-06-25 13:38:38
Original
2380 people have browsed it

1. The role of proxy mode

The role of proxy mode is to provide a proxy for other objects to control access to this object.

In some cases, a client does not want or cannot directly reference another object, and the proxy object can play the role of intermediary between the client and the target object.

2. Roles generally involved in the proxy model

Abstract role: Declare the common interface between the real object and the proxy object.

Agent role: The proxy object role contains a reference to the real object internally, so that the real object can be manipulated. At the same time, the proxy object provides the same interface as the real object so that it can replace the real object at any time. object. At the same time, the proxy object can add other operations when performing operations on the real object, which is equivalent to encapsulating the real object.

Real role: The real object represented by the proxy role is the object we ultimately want to reference.

#Illustration: The following program uses a static proxy to implement an uppercase letter conversion function.

Interface class ISomeService:

package com.ietree.basicskill.designpattern.staticproxy;/**
 * 接口类
 * 
 * @author Root */public interface ISomeService {
    
    String doFirst();    void doSecond();
}
Copy after login

Implementation class SomeServiceImpl:

package com.ietree.basicskill.designpattern.staticproxy;/**
 * 实现类
 * 
 * @author Root */public class SomeServiceImpl implements ISomeService {

    @Overridepublic String doFirst() {
        System.out.println("执行doFirst()...");
        String result = "abcde";return result;
    }

    @Overridepublic void doSecond() {
        System.out.println("执行doSecond()...");
    }

}
Copy after login

Proxy class SomeServiceProxy:

  SomeServiceProxy =
Copy after login

Test class Main:

package com.ietree.basicskill.designpattern.staticproxy;public class Main {public static void main(String[] args) {        ISomeService service = new SomeServiceProxy();String result = service.doFirst();
        System.out.println(result);
        
        service.doSecond();
    }
}
Copy after login

3. Summary

If you want to use the proxy mode in the above way (static proxy), then the real role The implementation must already exist and use it as an internal property of the proxy object.

But in actual use, a real role must correspond to a proxy role, but if used in large quantities, it will lead to a sharp expansion of the class; in addition, if the real role is not known in advance, how to use the proxy? This problem can be solved by Java's dynamic proxy class.

The above is the detailed content of The role of the proxy pattern in design patterns. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template