Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code repetition in Flyweight/README.md #2546

Closed
rajivaPavan opened this issue Jul 1, 2023 · 10 comments · Fixed by #2926
Closed

Code repetition in Flyweight/README.md #2546

rajivaPavan opened this issue Jul 1, 2023 · 10 comments · Fixed by #2926
Labels
epic: pattern info: help wanted status: stale issues and pull requests that have not had recent interaction type: refactoring

Comments

@rajivaPavan
Copy link

potions.put(type, potion);

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.

@ShivanshCharak
Copy link
Contributor

LOL nice, i wanna work on this issue

@ShivanshCharak
Copy link
Contributor

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

@rajivaPavan
Copy link
Author

An exception can be thrown in the default case

@ShivanshCharak
Copy link
Contributor

Exception for what, Invalid argument?

@rajivaPavan
Copy link
Author

rajivaPavan commented Jul 14, 2023

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

@ShivanshCharak
Copy link
Contributor

ShivanshCharak commented Jul 14, 2023

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

@stale
Copy link

stale bot commented Sep 18, 2023

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 stale bot added the status: stale issues and pull requests that have not had recent interaction label Sep 18, 2023
@Gauravkumar1502
Copy link

Gauravkumar1502 commented Sep 19, 2023

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.

@stale stale bot removed the status: stale issues and pull requests that have not had recent interaction label Sep 19, 2023
@Khomendra
Copy link

is this issue still open?

Copy link

stale bot commented Dec 6, 2023

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 stale bot added the status: stale issues and pull requests that have not had recent interaction label Dec 6, 2023
samuelpsouza added a commit to samuelpsouza/java-design-patterns that referenced this issue Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
epic: pattern info: help wanted status: stale issues and pull requests that have not had recent interaction type: refactoring
Projects
Development

Successfully merging a pull request may close this issue.

5 participants