Discover how simple patterns can turn your tangled code into a smooth-running machine!
Why When to use which structural pattern in LLD? - Purpose & Use Cases
Imagine building a complex software system by manually connecting every part without a clear plan. You try to fit pieces together like a puzzle, but each piece is different in size and shape. You end up with a messy, tangled system that is hard to understand or change.
Doing this by hand is slow and confusing. Without clear patterns, you make mistakes, duplicate code, and struggle to add new features. The system becomes fragile and breaks easily when you try to fix or improve it.
Structural patterns give you a clear way to organize parts of your system. They act like blueprints showing how to connect pieces so they fit well and work together smoothly. This makes your system easier to build, understand, and change.
class Engine { void start() {} } class Car { Engine engine; void start() { engine.start(); } } // Directly using Engine inside Car without pattern
interface VehiclePart {
void start();
}
class Engine implements VehiclePart {
public void start() {}
}
class Car {
VehiclePart engine;
public void start() { engine.start(); }
}
// Using interface to decouple partsWith the right structural pattern, you can build flexible systems that grow and adapt without breaking.
Think of a company organizing teams: using clear roles and communication paths (patterns) helps everyone work together smoothly, even as the company grows.
Manual connections lead to messy, fragile systems.
Structural patterns provide clear, reusable ways to organize parts.
Choosing the right pattern makes your system flexible and maintainable.
Practice
Solution
Step 1: Understand the problem context
Part-whole hierarchies like folders and files form tree structures where each node can be a composite or a leaf.Step 2: Match the pattern to the structure
The Composite pattern is designed to treat individual objects and compositions uniformly, perfect for tree-like structures.Final Answer:
Composite pattern -> Option BQuick Check:
Tree structures = Composite pattern [OK]
- Confusing Decorator with Composite
- Using Proxy for hierarchy management
- Choosing Facade for internal structure
Solution
Step 1: Identify the need to add features dynamically
When you want to add new behavior to objects at runtime without altering their class, you need a flexible pattern.Step 2: Select the pattern that wraps objects
The Decorator pattern wraps objects to add responsibilities dynamically, without changing the original class.Final Answer:
Decorator pattern -> Option DQuick Check:
Add features dynamically = Decorator [OK]
- Choosing Proxy instead of Decorator
- Using Facade to add features
- Confusing Composite with Decorator
class ResourceProxy {
constructor() {
this.resource = null;
}
access() {
if (!this.resource) {
this.resource = new RealResource();
}
return this.resource;
}
}Solution
Step 1: Analyze the code behavior
The code delays creating the real resource until it is accessed, controlling access and adding lazy loading.Step 2: Identify the matching pattern
This behavior matches the Proxy pattern, which controls access to another object and can add lazy initialization.Final Answer:
Proxy pattern -> Option AQuick Check:
Control access + lazy load = Proxy [OK]
- Confusing Proxy with Decorator
- Using Facade for access control
- Choosing Composite for resource control
class ComplexSystem {
operationA() {}
operationB() {}
operationC() {}
}
class SimpleInterface {
constructor() {
this.system = new ComplexSystem();
}
simpleOperation() {
this.system.operationA();
this.system.operationB();
this.system.operationC();
}
}
What is the likely mistake?Solution
Step 1: Understand Facade pattern purpose
Facade provides a simple interface hiding complex subsystem details from clients.Step 2: Identify the mistake in code usage
If clients still see complex calls, likely the ComplexSystem instance is exposed or clients bypass the Facade.Final Answer:
They did not hide the ComplexSystem instance properly -> Option AQuick Check:
Facade hides complexity = hide subsystem [OK]
- Confusing Proxy with Facade
- Exposing subsystem objects to clients
- Using Decorator to simplify interface
Solution
Step 1: Match patterns to requirements
Groups of shapes form tree structures best handled by Composite. Adding features dynamically fits Decorator. Controlling access fits Proxy.Step 2: Combine patterns correctly
Use Composite for shape groups, Decorator to add border color dynamically, and Proxy to control access to shapes.Final Answer:
Composite for groups, Decorator for features, Proxy for access control -> Option CQuick Check:
Groups=Composite, Features=Decorator, Access=Proxy [OK]
- Mixing Facade with group representation
- Using Proxy for adding features
- Confusing Decorator with access control
Ready to prove it?
Pass the test and this pattern moves unknown
