DEV Community

Higor Diego
Higor Diego

Posted on

Pattern - Flyweight

Pattern Flyweight

The Flyweight pattern is a software design pattern that was introduced by GoF (Gang of Four) in the book "Design Patterns: Elements of Reusable Object-Oriented Software". The purpose of this pattern is to help minimize memory usage by allowing identical objects to be shared between various parts of the application, rather than creating independent objects for each instance.

The Flyweight pattern is a software design pattern that allows you to share identical objects instead of creating an independent instance for each use. This is done to reduce memory usage and improve performance, especially in large applications or applications with many similar objects. The pattern separates shared state, which is stored in the Flyweight object, from unshared state, which is maintained by the client. The Flyweight pattern is useful for optimizing applications that have many identical objects with small variations.

The Flyweight pattern is useful in applications that need to deal with many similar objects, especially when there are performance and memory usage concerns. Some common examples include:

  • Games: where there are many similar characters on the screen, such as enemies or soldiers.
  • Graphical user interface applications: where there are many similar controls on the screen, such as buttons or text boxes.
  • Financial systems: where there are many similar transactions, such as bank transfers or purchases.

The Flyweight pattern is an effective approach to optimizing applications that handle many similar objects, allowing you to share identical objects instead of creating independent instances for each use.

Below is a simple code example using the Flyweight pattern.

class Coffee {
   constructor(flavor, price) {
     this.flavor = flavor;
     this.price = price;
   }
}

class CoffeeShop {
   constructor() {
     this.coffees = [];
   }

   takeOrder(flavor, table) {
     this.coffees[table] = CoffeeFlavorFactory.getCoffeeFlavor(flavor);
     console.log("Coffee flavor of table " + table + " is " + this.coffees[table].getFlavor());
   }
}

class CoffeeFlavorFactory {
   constructor() {
     this.flavors = [];
   }

   static getCoffeeFlavor(flavor) {
     if (!this.flavors[flavor]) {
       this.flavors[flavor] = new Coffee(flavor, Math.floor(Math.random() * 10));
     }
     return this.flavors[flavor];
   }

   getTotalCoffeeFlavorsMade() {
     return this.flavors.length;
   }
}

let coffeeShop = new CoffeeShop();

coffeeShop.takeOrder("Cappuccino", 1);
coffeeShop.takeOrder("Cappuccino", 2);
coffeeShop.takeOrder("Espresso", 3);
coffeeShop.takeOrder("Cappuccino", 4);
coffeeShop.takeOrder("Espresso", 5);

console.log("Total Coffee Flavors Made: " + CoffeeFlavorFactory.getTotalCoffeeFlavorsMade());

Enter fullscreen mode Exit fullscreen mode

In this example, we create a Coffee class that represents a type of coffee shop and a CoffeeShop class that represents the coffee shop. The CoffeeFlavorFactory class is used to create shared Coffee objects. When a customer places an order, the coffee shop calls the takeOrder method of the CoffeeShop class, which in turn calls the getCoffeeFlavor method of the CoffeeFlavorFactory class. If the Coffee object has already been created, it will be returned, otherwise a new object will be created. In this way, the Flyweight pattern is implemented and memory usage is optimized.

Simple, right?

There are several advantages to using the Flyweight pattern which include:

  • Memory optimization: The Flyweight pattern allows similar objects to share data and resources, which helps reduce memory usage.
  • Better performance: In addition to optimizing memory usage, the Flyweight pattern can also improve the overall performance of the application, as it is faster to create and retrieve shared objects than to create new objects.
  • Greater flexibility: The Flyweight pattern allows shared objects to be used in different contexts, which increases application flexibility.
  • Code reuse: With the Flyweight pattern, it is possible to reuse common code among similar objects, which can lead to easier and more reliable code maintenance.
  • Less complexity: When dealing with similar objects, the Flyweight pattern simplifies the application's code and logic, making it easier to understand and maintain.

Conclusion

The Flyweight pattern is a software design pattern that optimizes memory usage and improves performance in applications that deal with many similar objects.

Hope this helps, until next time.

Top comments (1)

Collapse
 
arashrah profile image
Arash Rahimi

Good article, I enjoyed reading it! If you're interested, feel free to check out my articles as well