Prototype Vs. Flyweight Design Patterns
Asked Answered
K

3

16

I need to find out some differences between Prototype D.P and Flyweight D.P. I know that the basic difference is that the former makes deep copy. Whereas the letter makes shared object. My lecturer said that there are more differences.

Someone know the others?

Karissakarita answered 19/12, 2015 at 9:13 Comment(4)
flyweight is a pattern to share underlying data between instances to save space and time. Prototype is a creation-abstraction pattern. So they refer to, and are used for, different thingsBeset
GOF design-patterns book reference uml.org.cn/c++/pdf/DesignPatterns.pdf, Prototype at page 133, Flyweight at page 218Beset
I know the both of them. But I can't understand the difference between them expected what I said.Karissakarita
JS prototypical inheritance is pretty similar to flyweights, but the prototype pattern is a different thing altogether.Vigil
M
17

First of all they belong to different categories: Prototype is creational one, Flyweight is structural one.

In Prototype objects' creation go through cloning, it ease object's creation. By making a request for cloning we create new cloned object each time.

In Flyweight by making a request we try to reuse as much objects as possible by sharing them. New required object will be created if we don't find such one. It's being done for resource optimization.

While in Prototype we could clone even one object, Flyweight pattern makes sense to use when in the application we use big number of objects.

All described affect on implementation side as well.

Meaganmeager answered 20/12, 2015 at 11:10 Comment(0)
P
5

In Flyweight, object is immutable.
In Prototype, object is mutable.

Flyweight is about saving memory by not creating new objects and reusing existing ones when possible.
Prototype is about, reusing existing object in order to save cost of new object creation.

Flyweight is used when creating multiple type of single object.
Prototype is used when creating single type of single object.

Pelag answered 2/5, 2018 at 13:24 Comment(1)
GoF’s book never defined flyweights as immutable. They can be mutable.Porbeagle
K
0

Flyweight objects are immutable. Once created, its state can't be changed. Due to this flyweight objects can be used by multiple clients without any issue whereas in an object pool an object can be used only by one client at a time.

The prototype will create duplicate objects by cloning but its state needs not to be immutable. It's just creating another object on demand from a given template. i.e, we are creating an object here so it will come under creational pattern.

In Flyweight you will not create a new object but an immutable object will be shared accross the clients.

Klaus answered 30/4, 2022 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.