Lazy loading is a concept where we delay the loading of object until the point where we need it.
- Lazy loading is just a fancy name given to the process of initializing a class when it’s actually needed.
- In simple words, Lazy loading is a software design pattern where the initialization of an object occurs only when it is actually needed and not before to preserve simplicity of usage and improve performance.
- Lazy loading is essential when the cost of object creation is very high and the use of the object is very rare. So this is the scenario where it’s worth implementing lazy loading.The fundamental idea of lazy loading is to load object/data when needed.
For Example, Suppose You are creating an application in which there is a Company object and this object contains a list of employees of the company in a ContactList object. There could be thousands of employees in a company. Loading the Company object from the database along with the list of all its employees in the ContactList object could be very time consuming. In some cases you don’t even require the list of the employees, but you are forced to wait until the company and its list of employees loaded into the memory.
One way to save time and memory is to avoid loading of the employee objects until required and this is done using the Lazy Loading Design Pattern.
There are four common implementations of Lazy Loading pattern :
- Virtual proxy
- Lazy initialization
- Value holder
The Virtual Proxy pattern is a memory saving technique that recommends postponing an object creation until it is needed. It is used when creating an object the is expensive in terms of memory usage or processing involved.
Company Name: ABC Company Company Address: India Company Contact No.: +91-011-28458965 Requesting for contact list Fetching list of employees Employee Name: Lokesh, EmployeeDesignation: SE, Employee Salary: 2565.55 Employee Name: Kushagra, EmployeeDesignation: Manager, Employee Salary: 22574.0 Employee Name: Susmit, EmployeeDesignation: G4, Employee Salary: 3256.77 Employee Name: Vikram, EmployeeDesignation: SSE, Employee Salary: 4875.54 Employee Name: Achint, EmployeeDesignation: SE, Employee Salary: 2847.01
Now, In the above code have a Company object is created with a proxy ContactList object. At this time, the Company object holds a proxy reference, not the real ContactList object’s reference, so there no employee list loaded into the memory.
The Lazy Initialization technique consists of checking the value of a class field when it’s being used. If that value equals to null then that field gets loaded with the proper value before it is returned.
Here is the example :
Number of instances made = 1 BMW Number of instances made = 2 BMW Audi Number of instances made = 2 BMW Audi
Basically, A value holder is a generic object that handles the lazy loading behavior and appears in place of the object’s data fields.When the user needs to access it, they simply ask the value holder for its value by calling the GetValue method. At that time (and only then), the value gets loaded from a database or from a service.(this is not always needed).
Note : The main drawback of this approach is that the user has to know that a value holder is expected.
A ghost is the object that is to be loaded in a partial state. It corresponds to the real object but not in its full state. It may be empty or it may contain just some fields (such as the ID). When the user tries to access some fields that haven’t been loaded yet, the ghost object fully initializes itself (this is not always needed).
For example, Let’s consider that a developer what add an online form so that any user can request content via that online form. At the time of creation all we know is that content will be accessed but what action or content is unknown to the user.
In the above PHP example, the content from the online form can be accessed to the user in the form of text file or any source.
- UID is the unique id for the every particular user.
- requestTime is the time when user requested the content from the online form.
- dataType is the type of data. Mostly it is text but depends on the form.
- request is the boolean function to notify the user about the request being completed or not.
- This approach is a faster application start-up time as it is not required to created and load all of the application objects.
- The code becomes complicated as we need to check if loading is needed or not. So this may cause slower in the performance.
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.