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?
Applying the pattern lets you clone complex structures instead of re-constructing them from scratch. Prototype isn't based on inheritance, so it doesn't have its drawbacks. On the other hand, Prototype requires a complicated initialization of the cloned object.
Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Flyweight pattern is used when we need to create a large number of similar objects (say 105). One important feature of flyweight objects is that they are immutable. This means that they cannot be modified once they have been constructed.
Prototype allows us to hide the complexity of making new instances from the client. The concept is to copy an existing object rather than creating a new instance from scratch, something that may include costly operations.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With