Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype Vs. Flyweight Design Patterns

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?

like image 682
Matan Avatar asked Dec 19 '15 09:12

Matan


People also ask

How does pattern differ from prototype?

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.

What does prototype design pattern mean?

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.

When would you use the Flyweight pattern?

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.

What is advantage of prototype design pattern?

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.


2 Answers

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.

like image 121
Mk.Sl. Avatar answered Sep 22 '22 00:09

Mk.Sl.


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.

like image 31
CourseTriangle Avatar answered Sep 24 '22 00:09

CourseTriangle