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

Design Patterns · Expert · question 70 of 100

Explain how the Builder pattern can be adapted to handle concurrent object creation in a multi-threaded environment.?

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

The Builder pattern can be used to create complex objects by separating the object creation logic from their representation. This pattern can also be adapted to handle concurrent object creation in a multi-threaded environment by implementing the Builder as follows:

1. Use thread-local storage: Create a Builder instance for each thread and use thread-local storage to store it. This way, each thread can have its own instance of the Builder and can use it independently to create objects without interfering with other threads.

2. Synchronize the build method: To ensure that only one thread can build an object at a time, the build method can be synchronized. This way, if multiple threads attempt to build objects simultaneously, only one thread will be allowed to build the object while the others wait.

3. Use immutable objects: The objects created by the Builder pattern should preferably be immutable, i.e., their internal state should not change after creation. This makes them safe to use in a multi-threaded environment without any additional synchronization.

Here is an example implementation of the Builder pattern adapted for concurrent object creation in Java:

public class ComplexObject {
    // fields and methods of the complex object
}

public class ComplexObjectBuilder {
    private int field1;
    private String field2;
    // other fields of the builder

    public ComplexObjectBuilder setField1(int field1) {
        this.field1 = field1;
        return this;
    }

    public ComplexObjectBuilder setField2(String field2) {
        this.field2 = field2;
        return this;
    }
    // other setter methods

    public synchronized ComplexObject build() {
        // create and return the complex object 
        ComplexObject obj = new ComplexObject(field1, field2, ...);
        return obj;
    }
}

public class ClientThread implements Runnable {
    private ThreadLocal<ComplexObjectBuilder> builderThreadLocal;

    public void run() {
        // get the builder instance for this thread
        ComplexObjectBuilder builder = builderThreadLocal.get();
        // modify the builder state to create the object
        builder.setField1(1).setField2("value");
        // build the object
        ComplexObject obj = builder.build();
        // use the object
        // ...
    }
}

In this implementation, each client thread has its own instance of the ComplexObjectBuilder stored in a ThreadLocal variable called builderThreadLocal. The build() method of the builder is synchronized to ensure thread-safety when creating objects. Finally, the ComplexObject created by the builder is immutable, thus making it thread-safe for concurrent use without any additional synchronization.

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