Proxy mode use the proxy object to complete the user request, shielding the user’s access to the real object. Real-world agents are empowered to carry out some matters of the parties without the need for the parties to come forward. From a third-party perspective, it seems that the parties do not exist because they only communicate with the agents. In fact, agents must have the authority of the parties, and on the core issues also need to ask the parties.
There are also a number of intent to use the proxy mode in software design, such as the need to shield the client from direct access to real objects for security reasons, or the technical details (such as RMI) of using proxy classes to handle remote method calls in remote calls Improve system performance, and encapsulate real objects so as to achieve the purpose of lazy loading.
Agent mode role is divided into four kinds:
Thematic Interfaces: A public external method that defines proxy classes and real topics, and is also a way to represent the true topic of proxy classes;
The real theme: the real business logic class;
Agent category: used to proxy and package the real theme;
Main: Client, using proxy classes and thematic interface to do some work.
A simple example to explain the use of proxy mode lazy loading method and its significance. Suppose a client software based on user requests to the database query data. Before querying data, you need to get the database connection, initialize all the classes of the system when the software is turned on, and try to get the database connection.
When the system has a large number of similar operations exist (such as XML parsing, etc.), all of these initialization operations will make the system start very slow. To this end, the use of the proxy mode proxy encapsulation of the database query initialization operation, when the system starts, initialize this proxy class, rather than the real database query class, and the proxy class did not do anything. Therefore, its construction is quite rapid.
When the system is started, the method that consumes the most resources is separated by the proxy mode, which can speed up the startup of the system and reduce the waiting time of the user. In the real user query operation by the proxy class alone to load the real database query class to complete the user’s request. This process is to use the proxy mode to achieve a lazy loading.
Core idea behind deferred loading Proxysite
The core idea behind deferred loading is that if you do not currently use this component, you do not need to actually initialize it, use a surrogate object instead of its original location, and load it whenever you really need it. It is very meaningful to use the lazy loading of the proxy mode.
Firstly, it can distribute the system pressure on the timeline. In particular, it does not have to complete all the initialization tasks at the system startup, so as to speed up the startup time. Secondly, for many real themes, May not be called at all until the software is started until it is shut down. Initializing these data is a waste of resources.
For example, using the proxy class encapsulates the database query class, the system startup process for this example. If the system does not use the proxy mode, it is necessary to initialize the DB Query object at startup, and use the proxy mode, only need to initialize a lightweight object DB Query Proxy at startup.
The following code ID Query is the thematic interface, defines the proxy class and the real class needs external services, defines the common method request () function to achieve database queries. DB Query is the real theme, responsible for the actual business operations, DB Query proxy class.