Posts

Showing posts with the label Structural Patterns

Understanding Flyweight Design Pattern in Java

Image
The Flyweight Design Pattern is a powerful structural design pattern in Java used to reduce memory usage when dealing with a large number of similar objects. Instead of creating a new object every time, this pattern reuses shared objects , improving performance, memory efficiency, and scalability. If you work with applications like editors, games, data visualization tools, or map rendering, understanding the Flyweight Pattern is extremely useful. What is the Flyweight Pattern? The Flyweight Pattern allows an application to support a large number of fine-grained objects by sharing common data. It splits object state into: Intrinsic State – shared, reusable, constant Extrinsic State – specific to each object and supplied externally This allows the system to hold fewer objects in memory. Real-Time Use Cases of Flyweight Pattern Text editors storing repeated characters Game engines rendering millions of objects Map applications with repeated markers Cach...

Understanding Composite Design Pattern in Java

Image
The Composite Design Pattern is one of the most powerful structural design patterns in Java . It is widely used when you want to represent hierarchical structures such as trees — for example, a folder structure, UI components, organization charts, menus in web apps, and more. In this blog, we will understand what the Composite Pattern is, when to use it, its advantages, and a real-time Java example. If you are preparing for Java interviews, system design, or scalable application development , this article will help you. What is the Composite Design Pattern in Java? The Composite Pattern allows you to treat individual objects and a group of objects in the same way . It is mainly used to implement tree-like structures . Every node in the tree can be either: Leaf – an individual object Composite – a container that holds leaf or other composite objects The main objective is to let the client treat both leaf and composite objects uniformly . Real-Time Use Cases Fil...