diff --git a/flyweight/README.md b/flyweight/README.md index 469bb8626..9a0e13e19 100644 --- a/flyweight/README.md +++ b/flyweight/README.md @@ -16,7 +16,100 @@ tags: Use sharing to support large numbers of fine-grained objects efficiently. -![alt text](./etc/flyweight_1.png "Flyweight") +## Explanation +Real world example + +> Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create new object for each of them. Instead one object instance can represent multiple shelf items so memory footprint remains small. + +In plain words + +> It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects. + +Wikipedia says + +> In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. + +**Programmatic example** + +Translating our alchemist shop example from above. First of all we have different potion types + +``` +public interface Potion { + void drink(); +} + +public class HealingPotion implements Potion { + private static final Logger LOGGER = LoggerFactory.getLogger(HealingPotion.class); + @Override + public void drink() { + LOGGER.info("You feel healed. (Potion={})", System.identityHashCode(this)); + } +} + +public class HolyWaterPotion implements Potion { + private static final Logger LOGGER = LoggerFactory.getLogger(HolyWaterPotion.class); + @Override + public void drink() { + LOGGER.info("You feel blessed. (Potion={})", System.identityHashCode(this)); + } +} + +public class InvisibilityPotion implements Potion { + private static final Logger LOGGER = LoggerFactory.getLogger(InvisibilityPotion.class); + @Override + public void drink() { + LOGGER.info("You become invisible. (Potion={})", System.identityHashCode(this)); + } +} +``` + +Then the actual Flyweight object which is the factory for creating potions + +``` +public class PotionFactory { + + private final Map potions; + + public PotionFactory() { + potions = new EnumMap<>(PotionType.class); + } + + Potion createPotion(PotionType type) { + Potion potion = potions.get(type); + if (potion == null) { + switch (type) { + case HEALING: + potion = new HealingPotion(); + potions.put(type, potion); + break; + case HOLY_WATER: + potion = new HolyWaterPotion(); + potions.put(type, potion); + break; + case INVISIBILITY: + potion = new InvisibilityPotion(); + potions.put(type, potion); + break; + default: + break; + } + } + return potion; + } +} +``` + +And it can be used as below + +``` +PotionFactory factory = new PotionFactory(); +factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818) +factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364) +factory.createPotion(PotionType.INVISIBILITY).drink(); // You become invisible. (Potion=6566818) +factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489) +factory.createPotion(PotionType.HOLY_WATER).drink(); // You feel blessed. (Potion=1104106489) +factory.createPotion(PotionType.HEALING).drink(); // You feel healed. (Potion=648129364) +``` ## Applicability The Flyweight pattern's effectiveness depends heavily on how diff --git a/flyweight/etc/flyweight.png b/flyweight/etc/flyweight.png deleted file mode 100644 index 265d03924..000000000 Binary files a/flyweight/etc/flyweight.png and /dev/null differ diff --git a/flyweight/etc/flyweight.ucls b/flyweight/etc/flyweight.ucls deleted file mode 100644 index 633e385b8..000000000 --- a/flyweight/etc/flyweight.ucls +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/flyweight/etc/flyweight.urm.puml b/flyweight/etc/flyweight.urm.puml deleted file mode 100644 index 648c223af..000000000 --- a/flyweight/etc/flyweight.urm.puml +++ /dev/null @@ -1,66 +0,0 @@ -@startuml -package com.iluwatar.flyweight { - class AlchemistShop { - - LOGGER : Logger {static} - - bottomShelf : List - - topShelf : List - + AlchemistShop() - + enumerate() - - fillShelves() - + getBottomShelf() : List - + getTopShelf() : List - } - class App { - + App() - + main(args : String[]) {static} - } - class HealingPotion { - - LOGGER : Logger {static} - + HealingPotion() - + drink() - } - class HolyWaterPotion { - - LOGGER : Logger {static} - + HolyWaterPotion() - + drink() - } - class InvisibilityPotion { - - LOGGER : Logger {static} - + InvisibilityPotion() - + drink() - } - class PoisonPotion { - - LOGGER : Logger {static} - + PoisonPotion() - + drink() - } - interface Potion { - + drink() {abstract} - } - class PotionFactory { - - potions : Map - + PotionFactory() - ~ createPotion(type : PotionType) : Potion - } - enum PotionType { - + HEALING {static} - + HOLY_WATER {static} - + INVISIBILITY {static} - + POISON {static} - + STRENGTH {static} - + valueOf(name : String) : PotionType {static} - + values() : PotionType[] {static} - } - class StrengthPotion { - - LOGGER : Logger {static} - + StrengthPotion() - + drink() - } -} -AlchemistShop --> "-topShelf" Potion -HealingPotion ..|> Potion -HolyWaterPotion ..|> Potion -InvisibilityPotion ..|> Potion -PoisonPotion ..|> Potion -StrengthPotion ..|> Potion -@enduml \ No newline at end of file diff --git a/flyweight/etc/flyweight_1.png b/flyweight/etc/flyweight_1.png deleted file mode 100644 index f30225aa0..000000000 Binary files a/flyweight/etc/flyweight_1.png and /dev/null differ diff --git a/pom.xml b/pom.xml index cf1bf9831..fc4984802 100644 --- a/pom.xml +++ b/pom.xml @@ -471,6 +471,7 @@ composite decorator facade + flyweight