Detailed explanation of solving the problem of wine distribution based on Java agent mode

This article illustrates how Java solves the problem of wine distribution based on agent mode. Share with you for your reference, as follows:

I Schema definition

In the proxy mode, two objects participate in processing the same request. The received request is entrusted to the real object by the proxy object. The proxy object controls the access of the request. It acts as an intermediary bridge between the client application and the real object. Proxy mode uses object aggregation instead of inheritance, which effectively reduces the coupling between software modules.

II Model example

1 mode analysis

The first mock exam is to explain the pattern by using red wine distribution.

2. Static class diagram of agent mode

3 code example

3.1 red wine factory interface iredwine

3.2 the red wine factory implements a realredwinefactory

3.3 create red wine agent - redwineproxy

3.4 buy a bottle of red wine from a red wine agent

4 operation results

III Design principle of this mode

1 delay loading to improve system efficiency

2. Principle of single responsibility

IV Use occasion

1 remote proxy: provide local proxy for an object in different address spaces.

2 virtual proxy: if the creation of an object is very time-consuming, it can be called through the proxy object. Before the creation of the real object, a false call is returned. When the real object is created, the corresponding method call of a real object is returned to the customer.

3. The protection agent controls the access of the original object.

4 instead of simple pointers, intelligent guidance performs some additional operations when accessing objects.

V Proxy pattern static class diagram

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

Real role: the target object that actually handles the request.

Proxy role: the proxy object role contains references to real objects, so that the proxy object can turn requests into real objects for processing. At the same time, the proxy object can add additional operations before and after the real object operation.

For more Java related content, interested readers can view the special topics of this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>