LOW LEVEL DESIGN · STEP 6 OF 9
Structural patterns
Seven ways to combine classes and objects into bigger structures that stay flexible. Each lesson is an animated walkthrough with a small Java example you can run, notes and the interview questions with answers.
- 1 Adapter patternWrap a class so its interface fits the one your code expects.The Adapter pattern explained with a fitness app that must read a third-party heart-rate band: the target, adaptee and adapter roles, translating units and errors, object adapter vs class adapter, wrapping a vendor SDK behind your own interface, when not to bother, Adapter vs Decorator, Proxy and Facade, and where it appears in Java and LLD interviews.
- 2 Bridge patternSplit two things that vary independently into two hierarchies joined by a reference.The Bridge pattern explained with shop documents (invoice, packing slip, gift note) printed through different renderers (HTML, receipt printer, Markdown): spotting two dimensions that vary independently, the abstraction and implementation hierarchies joined by composition, m plus n classes instead of m times n, switching at runtime, and Bridge vs Strategy and Adapter.
- 3 Composite patternTreat a single item and a group of items through the same interface.The Composite pattern explained with shipping cartons that hold articles and smaller cartons: component, leaf and composite roles, recursive totals with no instanceof checks, safe vs transparent designs, guarding against cycles, sealed interfaces in Java 17, folders, menus and org charts, Composite vs Decorator, and how it appears in Java UI toolkits and LLD interviews.
- 4 Decorator patternAdd behaviour by wrapping an object in another object with the same interface.The Decorator pattern explained with a webhook sender that gains retries, request signing and audit logging by wrapping instead of subclassing: the subclass explosion it prevents, component and decorator roles, stacking, why the order of wrappers matters, Java I/O streams as a real example, costs, and Decorator vs Adapter, Proxy, Facade and Composite.
- 5 Facade patternGive a complicated subsystem one simple front door.The Facade pattern through a food order desk: one placeOrder method over stock, wallet, kitchen, rider and SMS classes, with the undo steps kept in one place. Structure, Java 17 code, when not to use it, Facade vs Adapter and Mediator, the god object trap, SLF4J and JdbcTemplate, and where it appears in LLD interviews.
- 6 Flyweight patternShare the heavy, unchanging part of many small objects to save memory.The Flyweight pattern through a 60,000-seat stadium map: intrinsic state shared in four immutable SeatStyle objects, extrinsic state kept in compact arrays and passed in, a thread-safe factory, the memory maths, the Integer cache, Flyweight vs Singleton and object pools, and where it appears in LLD interviews.
- 7 Proxy patternPut a stand-in in front of an object to control access, cache or load lazily.The Proxy pattern through a hospital lab report service: a protection proxy that checks consent, a caching proxy, a lazy proxy that opens the archive on first use, remote proxies, JDK dynamic proxies with java.lang.reflect.Proxy, how Spring uses proxies, Proxy vs Decorator, and where proxies appear in LLD interviews.