Visit Sponsor

Written by 5:24 pm Design Patterns

Composite, Proxy & Flyweight Pattern Interview Questions in Java

Composite, Proxy, and Flyweight are important Structural Design Patterns in Java. These patterns are commonly asked in interviews because they solve real-world problems related to hierarchical structures, object access control, and memory optimization.

Q1. What is the primary purpose of the Composite Pattern?

Options:

  • A) To restrict object creation to a single instance
  • B) To compose objects into tree structures to represent part-whole hierarchies
  • C) To convert one interface into another compatible interface
  • D) To notify multiple dependent objects automatically

Answer:

B

Explanation:

  • A (Incorrect): This describes the Singleton Pattern.
  • B (Correct): The Composite Pattern is used to compose objects into tree structures so that individual objects and groups of objects can be treated uniformly.
  • C (Incorrect): This describes the Adapter Pattern.
  • D (Incorrect): This describes the Observer Pattern.

Q2. Which scenario is the best use case for the Composite Pattern?

Options:

  • A) Managing user login sessions
  • B) Representing shapes and groups of shapes in a drawing application
  • C) Converting XML data into JSON
  • D) Handling asynchronous notifications

Answer:

B

Explanation:

  • A (Incorrect): Login sessions are unrelated to Composite structures.
  • B (Correct): The Composite Pattern is ideal for representing a tree structure of objects, such as shapes and groups of shapes.
  • C (Incorrect): XML to JSON conversion is better suited for Adapter.
  • D (Incorrect): Notifications are commonly handled by Observer.

Q3. What is the primary purpose of the Proxy Pattern?

Options:

  • A) To provide a simplified interface to complex subsystems
  • B) To control access to an object by creating a surrogate
  • C) To manage the lifecycle of objects in a system
  • D) To optimize the performance of an application

Answer:

B

Explanation:

  • A (Incorrect): This describes the Facade Pattern, not the Proxy Pattern.
  • B (Correct): The Proxy Pattern creates a surrogate or placeholder for another object to control access to it.
  • C (Incorrect): Managing the lifecycle of objects is not the primary purpose of the Proxy Pattern.
  • D (Incorrect): While performance can be optimized, it is not the main focus of this pattern.

Q4. In the following code, which class represents the Proxy in the Proxy Pattern?

public class ImageHelper implements Image {
    private RealImage realImage;

    public void display() {
        if(realImage == null) {
            realImage = new RealImage();
        }
        realImage.display();
    }
}

Options:

  • A) RealImage
  • B) ImageHelper
  • C) Image
  • D) display

Answer:

B

Explanation:

  • B is correct as ImageHelper controls access to RealImage, deferring its instantiation.
  • A is incorrect since RealImage is the actual object, not the proxy.
  • C is incorrect because it is the interface type, not a specific implementation.
  • D is incorrect as it is a method, not a class.

Q5. What is the primary purpose of the Flyweight Pattern?

Options:

  • A) To minimize memory usage by sharing common data
  • B) To increase the complexity of code
  • C) To provide a way to create complex objects
  • D) To ensure object immutability

Answer:

A

Explanation:

  • A (Correct): The Flyweight Pattern aims to minimize memory usage by sharing common data among multiple objects.
  • B (Incorrect): It simplifies memory management, not increases complexity.
  • C (Incorrect): It focuses on sharing common data rather than creating complex objects.
  • D (Incorrect): Immutability is not a defined feature of the Flyweight Pattern.

Q6. Which of the following components are typically part of the Flyweight Pattern? (Select all that apply)

Options:

  • A) Flyweight Interface
  • B) Concrete Flyweight
  • C) Client
  • D) Flyweight Factory

Answer:

A, B, C, D

Explanation:

  • A (Correct): The Flyweight Interface defines the operations for Flyweight objects.
  • B (Correct): Concrete Flyweights implement the Flyweight interface and manage shared state.
  • C (Correct): The Client uses Flyweights to perform operations.
  • D (Correct): The Flyweight Factory creates and manages Flyweight objects.

Q7. Which statement best describes the key advantage of the Flyweight Pattern?

Options:

  • A) It improves security by limiting access to objects
  • B) It reduces memory usage when handling large numbers of similar objects
  • C) It creates complex objects step by step
  • D) It allows incompatible interfaces to work together

Answer:

B

Explanation:

  • A (Incorrect): Security control is related to Proxy.
  • B (Correct): Flyweight significantly reduces memory usage when many similar objects are required.
  • C (Incorrect): This describes Builder.
  • D (Incorrect): This describes Adapter.

Why These Patterns Matter in Java Interviews

Interviewers ask these patterns to test:

  • Tree structure design
  • Object composition
  • Controlled access
  • Lazy loading
  • Memory optimization
  • Reusability
  • Scalable architecture

Common Real-World Examples

Composite Pattern:

  • File systems
  • Organization charts
  • UI components
  • Menu hierarchies

Proxy Pattern:

  • Image lazy loading
  • Access control systems
  • Remote objects
  • Security wrappers

Flyweight Pattern:

  • Text editors
  • Game objects
  • Cache systems
  • Icon reuse

Quick Revision

  • Composite handles tree hierarchies.
  • Proxy controls access to another object.
  • Flyweight saves memory by sharing data.
  • All three belong to Structural Design Patterns.
Visited 3 times, 3 visit(s) today
Close