samedi 16 mai 2020

Flyweight design pattern getting Object parameter as its key?

I am trying to reduce duplicates of Book objects using Flyweight design pattern but got stuck at some point.

For example, assuming that there is a book class which contains some variables such as bookName, published_date and Langauge, and someone creates a book instance:

bookName: ABC // published_date: 17-05-2020 // Langauge: English.

What I am trying to do is, when he clones the same book instance above, I want to reduce the duplicates.

But, when I searched up for the flyweight pattern, they all get String or Integer as an intrinsic value / key.

Example:

class CoffeeFlavorFactory {

  private Map<String, CoffeeFlavor> flavors = new HashMap<String, CoffeeFlavor>();
   CoffeeFlavor getCoffeeFlavor(String flavorName) {
      CoffeeFlavor flavor = flavors.get(flavorName);
      if (flavor == null) { 
          flavor = new CoffeeFlavor(flavorName);
          flavors.put(flavorName, flavor);
      }
      return flavor;
 }

The above code gets String flavorName as an intrinsic value.

What I want to ask is, is there any way to get the Book object as an intrinsic value and use the flyweight pattern?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire