51 lines
3.1 KiB
Markdown
51 lines
3.1 KiB
Markdown
|
---
|
||
|
obj: concept
|
||
|
---
|
||
|
# Lazy Instantiation
|
||
|
|
||
|
## **Description:**
|
||
|
Lazy Instantiation is a design pattern that defers the creation of an object until it is actually needed. This pattern is particularly useful when creating or initializing an object is expensive in terms of time or resources, and you want to postpone it until the last possible moment to improve performance or save resources.
|
||
|
|
||
|
**How it's Used:**
|
||
|
1. **Identify Expensive Objects**: Determine which objects or resources in your application are expensive to create or initialize.
|
||
|
2. **Initialization on First Use**: Instead of creating these objects when the application starts, initialize them only when they are first requested by a client or when they are needed for a specific task.
|
||
|
3. **Lazy Initialization Mechanism**: Implement a mechanism to perform lazy initialization. Common techniques include using a null check, a flag, or a synchronized block to ensure that the object is created only once and when it's required.
|
||
|
4. **Client Code**: In your application, access or request the object as needed. The lazy initialization mechanism will ensure that the object is created the first time it's requested and reused for subsequent requests.
|
||
|
|
||
|
## **Example:**
|
||
|
Consider an application that manages a large collection of images, and loading each image into memory is resource-intensive. To save resources and improve performance, you can implement Lazy Instantiation for image loading:
|
||
|
|
||
|
- **Identify Expensive Objects**: Recognize that loading images into memory is resource-intensive.
|
||
|
- **Lazy Initialization Mechanism**: Create a class called `ImageLoader` that manages the loading of images. In this class, implement a lazy initialization mechanism for loading images on-demand.
|
||
|
```java
|
||
|
public class ImageLoader {
|
||
|
private Map<String, Image> loadedImages = new HashMap<>();
|
||
|
|
||
|
public Image getImage(String filename) {
|
||
|
if (!loadedImages.containsKey(filename)) {
|
||
|
// Load the image from disk or a remote source
|
||
|
Image image = loadImageFromFile(filename);
|
||
|
loadedImages.put(filename, image);
|
||
|
}
|
||
|
return loadedImages.get(filename);
|
||
|
}
|
||
|
|
||
|
private Image loadImageFromFile(String filename) {
|
||
|
// Code to load the image from disk or a remote source
|
||
|
// ...
|
||
|
return new Image(filename);
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
- **Client Code**: In your application, when you need to display an image, use the `ImageLoader` to obtain the image. The `ImageLoader` will load the image from the source only when requested and cache it for future use:
|
||
|
```java
|
||
|
ImageLoader imageLoader = new ImageLoader();
|
||
|
Image image1 = imageLoader.getImage("image1.jpg"); // Loaded when first requested
|
||
|
Image image2 = imageLoader.getImage("image2.jpg"); // Loaded when first requested
|
||
|
|
||
|
// Later in the application
|
||
|
Image image3 = imageLoader.getImage("image1.jpg"); // Reused from cache
|
||
|
```
|
||
|
|
||
|
By applying Lazy Instantiation in this example, you ensure that images are loaded into memory only when needed, conserving resources and improving the application's responsiveness. The images are cached for reuse, further enhancing performance when the same image is requested multiple times.
|