LLD interview question
Created on: Oct 14, 2024
-
What is prototype design pattern
Prototype Design Pattern is a creational pattern that enables the creation of new objects by copying an existing object medium
-
Builder Design Pattern
It is a design to construct a complex object step by step.
-
Factory design pattern
It is used when there is a need to create multiple objects of similar types, but the specific type of object to be created is determined at runtime.
-
Abstract factory
The Abstract Factory design pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It encapsulates the creation logic, allowing the client code to work with a factory interface rather than directly creating objects.
-
Adapter design pattern
-
facade design pattern
It is a Structural design pattern that provides a simplified interface or facade to a complex subsystem of classes or components
-
Flyweight design pattern
It aims to minimize memory usage by sharing as much data as possible across multiple objects
// Flyweight interface public interface Shape { void draw(); } // Concrete flyweight class public class Circle implements Shape { private final String color; public Circle(String color) { this.color = color; } @Override public void draw() { System.out.println("Drawing a circle with color: " + color); } } // Flyweight factory public class ShapeFactory { private final Map<String, Shape> circleMap; public ShapeFactory() { circleMap = new HashMap<>(); } public Shape getCircle(String color) { Shape circle = circleMap.get(color); if (circle == null) { circle = new Circle(color); circleMap.put(color, circle); } return circle; } } // Client code public class Main { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); Shape circle1 = shapeFactory.getCircle("red"); circle1.draw(); Shape circle2 = shapeFactory.getCircle("blue"); circle2.draw(); Shape circle3 = shapeFactory.getCircle("red"); circle3.draw(); // circle3 is the same instance as circle1, as it is retrieved from the flyweight factory } } -
Proxy design pattern
Use cases
- Lazy Loading: When dealing with resource-intensive objects, you may want to defer their creation until they are actually needed. The Proxy can create the real object only when it is necessary, improving performance.
- Access Control: The Proxy can enforce access control rules to restrict or grant access to an object based on user permissions.
- Remote Proxy: If you’re working with distributed systems, the Proxy can represent an object located in a remote server, providing a local interface to the client while handling communication with the remote object.4.Caching: The Proxy can store the results of expensive operations, allowing subsequent requests to be served from the cache rather than invoking the actual object.
-
Composite design pattern
The Composite design pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies.
The composite pattern is used when you want to represent a hierarchy of objects and need to treat both the individual objects and the groups of objects in a uniform way.
-
Bridge Design Pattern
Decouples an abstraction from its implementation so that the two can vary independently.
-
What are Singletons, write code to implement the same.
