WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Guru · question 90 of 100

Explain how the Builder pattern can be adapted to support advanced object creation scenarios, such as object pooling, lazy initialization, or object recycling.?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

The Builder pattern is a creational software design pattern that allows us to create objects by providing flexible steps to initialize them, without compromising on immutability. The basic idea behind the Builder design pattern is to separate the construction of objects from their representation. This separation optimizes the design, structuring, and testing of an object-oriented software system.

The basic version of the Builder pattern provides a set of methods which can be used to configure and construct a complex object with multiple optional components. But in advanced object creation scenarios, there could be additional requirements, such as object pooling, lazy initialization, and object recycling, to optimize resource usage and performance.

To support such scenarios, the Builder pattern can be adapted as follows:

### Object Pooling Scenario

Object pooling is a technique of reusing objects that have already been created, instead of creating new objects every time they are needed, thus reducing the burden of object creation and destruction. When using the builder pattern to create objects in this scenario, the builder would need to keep track of the objects that have already been created and reuse them where possible.

Here’s an example implementation of the builder pattern that supports object pooling:

public class Resource {
    // code for the resource class
}

public interface ResourceBuilder {
    public Resource build();
}

public class PooledResourceBuilder implements ResourceBuilder {
    private Resource resource;

    public PooledResourceBuilder() {
        // initialize the pool of objects
    }

    @Override
    public Resource build() {
        if (resource is available in pool) {
            return resource;
        } else {
            // create a new object and add it to the pool
            resource = new Resource();
            add resource to pool;
            return resource;
        }
    }
}

### Lazy Initialization Scenario

Lazy initialization is a technique of initializing objects only when they are actually needed, thus delaying their creation until that point. When using the builder pattern to create objects in this scenario, the builder would only initialize the object properties when they are first accessed.

Here’s an example implementation of the builder pattern that supports lazy initialization:

public class Resource {
    // code for the resource class
}

public interface ResourceBuilder {
    public void setProperty1(Property1 property1);
    public void setProperty2(Property2 property2);
    // more properties to be set
    public Resource build();
}

public class LazyResourceBuilder implements ResourceBuilder {
    private Property1 property1;
    private Property2 property2;
    // more properties

    @Override
    public void setProperty1(Property1 property1) {
        this.property1 = property1;
    }

    @Override
    public void setProperty2(Property2 property2) {
        this.property2 = property2;
    }

    // more setters

    @Override
    public Resource build() {
        Resource resource = new Resource();
        resource.setProperty1(this.property1);
        resource.setProperty2(this.property2);
        // set more properties
        return resource;
    }
}

### Object Recycling Scenario

Object recycling is a technique of reusing objects that were previously used and released, rather than creating new objects. When using the builder pattern to create objects in this scenario, the builder would not destroy the object when it is released, but keep it in a pool and reuse it when possible.

Here’s an example implementation of the builder pattern that supports object recycling:

public class Resource {
    // code for the resource class
}

public interface ResourceBuilder {
    public void setProperty1(Property1 property1);
    public void setProperty2(Property2 property2);
    // more properties to be set
    public Resource build();
    public void release(Resource resource);
}

public class RecyclableResourceBuilder implements ResourceBuilder {
    private Map<UUID, Resource> pool;
    private Property1 property1;
    private Property2 property2;
    // more properties

    public RecyclableResourceBuilder() {
        // initialize the pool of objects
    }

    @Override
    public void setProperty1(Property1 property1) {
        this.property1 = property1;
    }

    @Override
    public void setProperty2(Property2 property2) {
        this.property2 = property2;
    }

    // more setters

    @Override
    public Resource build() {
        if (pool is not empty) {
            // reuse a previous object
            Resource resource = pool.remove(0);
            resource.setProperty1(this.property1);
            resource.setProperty2(this.property2);
            // set more properties
            return resource;
        } else {
            // create a new object
            Resource resource = new Resource();
            resource.setProperty1(this.property1);
            resource.setProperty2(this.property2);
            // set more properties
            return resource;
        }
    }

    @Override
    public void release(Resource resource) {
        // reset the properties and add the object to the pool
        resource.reset();
        pool.add(resource);
    }
}

In summary, the Builder pattern can be adapted to support advanced object creation scenarios by adding additional logic to reuse and recycle objects, along with their properties, to optimize resource usage and performance.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics