What are the differences between Flyweight and Prototype patterns and where do you use Prototype patterns?

Asked 2 years ago, Updated 2 years ago, 125 views

If you compare the Flyweight and Prototype patterns,

  • Prototype reduces costs when constructor-based object generation costs are high
  • Flyweight shares instances and does not generate objects (unlike Flyweight, including clones).This reduces the cost of creating objects and cloning instances

I understand that there is a difference.Looking at this much, I thought Flyweight would cost less overall than Prototype, and I could do it including what Prototype can do.

So I have a question.

  • What is the difference between the Flyweight and Prototype patterns?
  • If Flyweight costs less overall than Prototype, and includes what Prototype can do, what would be the use of Prototype patterns?

I know that there is a difference between generative and structural patterns, but I am worried that both of them look the same.

Thank you for your cooperation.

Reference Site:
https://www.techscore.com/tech/DesignPattern/Prototype.html/
https://www.techscore.com/tech/DesignPattern/Flyweight.html/

design-pattern

2022-09-30 19:50

1 Answers

As mentioned in the questionnaire, there are the following major differences:

  • Prototype patterns generate objects every time
  • Flyweight patterns generate objects (only once).Use one object around.

Because the Flyweight pattern uses objects around, it is usually only available for immutable objects.
Prototype patterns, on the other hand, have no such restrictions.

For example, Linked , if you want each of the 100 snow crystals to have a different color (=Paper has a color field and you want to set a different value for each object), you cannot use the object around.
In other words, the Flyweight pattern is not applicable.

(Examples of Prototype patterns with the same link can certainly be realized with Flyweight patterns, so it's hard to tell the difference.)


2022-09-30 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.