Fake It Till You Make It — Lazy Load Through Virtual Proxy Pattern

Optimize resource allocation in your program

Harsh Jaiswal
4 min readApr 14, 2021
Title Image
Photo By Brett Jordan On Unsplash

Lazy Loading

Just imagine you are a chef in a restaurant and you have a habit of switching on the gas stove as soon as you enter the kitchen just so that you can save some time whenever you start working on the orders for the day but unfortunately some days you don’t receive even a single order . On such days ,this results in a wastage of fuel as your gas stove remains switched on for the entire day but you don’t really cook anything.

As you can see the problem with this approach is the improper utilization of available resources. Now imagine another scenario where you switch on the gas stove only when you receive your first order for the day . This approach would save a lot of your fuel that got wasted in the first approach. This is what is known as Lazy Loading.

According to the definition of Lazy Loading, the object initialization is deferred until the client actually needs it which ultimately improves the efficiency of the operation. So in the second approach of our restaurant example, we have deferred the gas stove initialization until we actually needed it i.e. until we received our first order for the day.

In the world of programming , lazy loading has the following four common ways of implementation :

  1. Lazy Initialization
  2. Virtual Proxy
  3. Value Holder
  4. Ghost Pattern

We would be focusing on the Virtual Proxy pattern for this article.

The Virtual Proxy Pattern

This is a design pattern that helps in optimizing the resource allocation by creating fake and lightweight copy of your real objects in a reusable manner and most often used in the following scenarios :

  1. When the object instantiation needs to perform some heavy configuration which consumes up a lot of your resources.
  2. When you only need to show a very minimal information about an object to the client until the client requests for more information.
  3. When certain operations on the real object are resource intensive , this pattern can be used as a caching mechanism.

I will be covering the first scenario in this article with the help of a java implementation of our restaurant example.

Want to read this story later? Save it in Journal.

Implementation

Consider an Order object that requires some resource intensive initialization. We want the object to be initialized only when we have to perform some operation related to that object and it should be available henceforth.

UML Diagram

In the above UML, the VirtualProxyEx file is acting as the client. We’ll create an Order interface having the function processOrder responsible for processing an order.

Next, we’ll create two files that will implement this interface. RealOrder and ProxyOrder. The resource intensive operation of initializing the order will be present in the RealOrder class while ProxyOrder will be a miniature copy of RealOrder .

As we can see from above that starting up of gas stove is present only in the RealOrder and the ProxyOrder will initialize our RealOrder object only when we call the function processOrder.

Runner file -

We have initialized three objects above. 2 proxyOrder objects and 1 realOrder objects. If we go by the above implementation we’ll see that even when we are not processing any order, orderWithoutProxy object will start up the gas stove (wastage of resources) while the proxyOrder object will start up only when we call the processOrder function. (Proper utilization of resources). The output of the above program will look like as below.

Console Output

So, with the help of our proxyOrder class, we have deferred the Order object initialization until we actually required processing the order. (variable order2 in the Runner file).

The above approach is one of the many ways we can use the Virtual Proxy pattern to perform lazy loading. To cover the other 2 scenarios mentioned above , I’ll drop a reference to external sources which clearly show how this pattern can be used in the respective scenarios.

Closing In

Thank you for reading the article all the way till here. I hope it must have helped you in learning a new design pattern which you’ll be coming across quite often throughout your programming journey. Just remember to Fake It Till You Make It 😉.

Wishing you a wonderful day ahead !!

References

  1. Lazy Loading definition
  2. Educative.io — Example for scenario 2
  3. DoFactory — Example for scenario 3

📝 Save this story in Journal.

--

--