Design Patterns_Proxy Patterns_Lazy Loading

Reprinted from: https://www.ibm.com/developerworks/cn/java/j-lo-proxy-pattern/index.html

A simple example is used to illustrate the method and meaning of lazy loading using the proxy pattern. Suppose a client software has the function of querying data in the database according to the user's request. Before querying data, you need to obtain a database connection. When the software is started, all classes of the system are initialized. At this time, try to obtain a database connection. When the system has a large number of similar operations (such as XML parsing, etc.), the superposition of all these initialization operations will make the system start up very slowly. To this end, the proxy class of the proxy mode is used to encapsulate the initialization operation in the database query. When the system starts, the proxy class is initialized instead of the real database query class, and the proxy class does nothing. Therefore, its construction is quite rapid.

When the system starts up, the proxy mode is used to separate the methods that consume the most resources, which can speed up the system startup speed and reduce the user's waiting time. When the user actually performs a query operation, the proxy class will load the real database query class alone to complete the user's request. This process is to use the proxy mode to implement lazy loading .

The core idea of ​​lazy loading is: if the component is not currently used, you don't need to actually initialize it, use a proxy object to replace its original position, and only load it when it is really needed . Lazy loading in proxy mode makes a lot of sense, firstly, it can spread the stress on the system on the timeline, especially when the system starts, without having to complete all the initialization work, thus speeding up the startup time; secondly, for many real subjects , it may not be called at all during the software startup until it is shut down. Initializing these data is undoubtedly a waste of resources. For example, after using the proxy class to encapsulate the database query class, the system startup process is an example. If the system does not use the proxy mode, the DBQuery object needs to be initialized at startup, but after using the proxy mode, only a lightweight object DBQueryProxy needs to be initialized at startup.

The following code IDBQuery is the subject interface, which defines the services that the proxy class and the real class need to provide externally, and defines the public method request() function for implementing database query. DBQuery is the real subject, responsible for the actual business operations, DBQueryProxy is the proxy class of DBQuery.

IDBQuery

1 public interface IDBQuery {
2  String request();
3 }

DBQuery

1  public  class DBQuery implements IDBQuery {
 2      public DBQuery() {
 3          try {
 4              Thread.sleep(1000); // Assume time-consuming operations such as database connection 
5          } catch (InterruptedException ex) {
 6              ex.printStackTrace();
 7          }
 8      }
 9      @Override
 10      public String request() {
 11          return "request string" ;
 12      }
 13 }

DBQueryProxy

1  public  class DBQueryProxy implements IDBQuery {
 2      private DBQuery real = null ; //We create a proxy object, which actually has only one uninitialized DBQuery
 3  
4      @Override
 5      public String request() {
 6          // Only when it is really needed Create a real object, the creation process may be slow 
7          if (real == null ) {
 8              real = new DBQuery();
 9          } // In a multi-threaded environment, a false class is returned here, similar to Future mode 
10          return real. request();
 11      }
 12}

Client

1  public  class Client {
 2      public  static  void main(String[] args) {
 3          IDBQuery q = new DBQueryProxy(); // Use proxy 
4          q.request(); // Create real object only when it is actually used 
5      }
 6 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325374373&siteId=291194637