java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Code repetition in Flyweight/README.md

Open rajivaPavan opened this issue 1 year ago • 10 comments

https://github.com/iluwatar/java-design-patterns/blob/5b147b00367a500e8dfeac882d4b0d6a0540c6b9/flyweight/README.md?plain=1#L85 The following code is quoted from the documentation of the flyweight design pattern (link above).

if (potion == null) {
      switch (type) {
        case HEALING -> {
          potion = new HealingPotion();
          potions.put(type, potion);
        }
        case HOLY_WATER -> {
          potion = new HolyWaterPotion();
          potions.put(type, potion);
        }
        case INVISIBILITY -> {
          potion = new InvisibilityPotion();
          potions.put(type, potion);
        }
        default -> {
        }
      }
    }

The following code line is repeating.

potions.put(type, potion);

Suggestion: It can be taken out of the switch-case statement.

rajivaPavan avatar Jul 01 '23 12:07 rajivaPavan

LOL nice, i wanna work on this issue

ShivanshCharak avatar Jul 14 '23 15:07 ShivanshCharak

wait i dont think moving that part out will work because, lets say if the input doesnot match the condition it will show default section which is nothing but if you move the "potions.put(type, potion);" out it will execute irrespective of the user input

ShivanshCharak avatar Jul 14 '23 16:07 ShivanshCharak

An exception can be thrown in the default case

rajivaPavan avatar Jul 14 '23 16:07 rajivaPavan

Exception for what, Invalid argument?

ShivanshCharak avatar Jul 14 '23 17:07 ShivanshCharak

Well yeah. But we don't need to consider a lot about the Exception since we are talking about design patterns here 😅

rajivaPavan avatar Jul 14 '23 17:07 rajivaPavan

Yeah if we create an exception there, it will halt the normal execution and will show the error on console which will ruin the whole code flow ig

ShivanshCharak avatar Jul 14 '23 17:07 ShivanshCharak

This issue has been automatically marked as stale because it has not had recent activity. The issue will be unassigned if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 18 '23 01:09 stale[bot]

This code snippet demonstrates the creation of a potion using the PotionType class. The PotionType is an enum with four fixed values. By utilizing the new Pattern Matching for Switch. The switch statement will be fully executed without requiring a default case.

Something like that :

if (potion == null) {
      potion = switch (type) {
        case HEALING -> new HealingPotion();
        case INVISIBILITY -> new InvisibilityPotion();
        case STRENGTH -> new StrengthPotion();
        case HOLY_WATER -> new HolyWaterPotion();
        case POISON -> new PoisonPotion();
      };
      potions.put(type, potion);
    }

Please let me know if I’m on the right track or if you have any further suggestions.

Gauravkumar1502 avatar Sep 19 '23 18:09 Gauravkumar1502

is this issue still open?

Khomendra avatar Oct 07 '23 08:10 Khomendra

This issue has been automatically marked as stale because it has not had recent activity. The issue will be unassigned if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 06 '23 22:12 stale[bot]