Difference between Prototype Design Pattern and Flyweight Design Pattern
Last Updated :
31 Oct, 2023
The major point in Prototype vs. Flyweight Design Pattern is that Prototype Design Pattern is a creational design pattern whereas Flyweight Design Pattern is a structural design pattern. In this post, we will look into this and more differences between the Prototype and Flyweight Design Patterns.
Let us begin with a basic understanding of each of them.
Prototype Design Pattern
The Prototype Design Pattern is a creational design pattern that provides a mechanism to create a new object by copying the original object rather than creating a new instance from scratch. In this case, the client does not need to know about the object creation process but only has references to the all-prototype object and can modify it according to its needs.
Why choose the prototype pattern:
- Cost of creating a new object
- Reducing complexity
- Time-consuming
- Avoiding the need for subclassing or inheritance
- Flexible object creation runtime
- Promote loose coupling
Flyweight Design Pattern
The flyweight pattern is the structural design pattern, and it provides ways to decrease object count through the reuse of existing objects. A flyweight pattern is used when we need a lot of similar objects
To apply the flyweight pattern, we need to separate the Instance property into Intrinsic and Extrinsic states:
- Intrinsic state: It is a Flyweight object and should be immutable and unchangeable.
- Extrinsic state: it is mutable and changeable. It makes a unique instance.
Differences between Prototype and Flyweight Design Patterns are as follows:
Prototype is a creational pattern. |
Flyweight is a structural pattern. |
In the prototype, we create a duplicate object by cloning. |
In Flyweight, we reuse the existing object. |
In the prototype, the object is mutable. |
In Flyweight, the object is immutable. |
Prototype is about saving cost by making a copy of the existing object. |
Flyweight is about saving memory by not creating new objects but rather reusing the existing ones. |
Prototype is used when the need is to create a single type of single object. |
Flyweight is used when the need is to create multiple types of single objects. |
We are creating an object here so it comes under a creational pattern. |
It is not a creation of objects but a sharing of them which is usually held in some external data structure, it is under a structural pattern. |
The basic difference is that the Prototype former makes deep copy. |
Whereas the Flyweight makes shared object and uses it. |
Share your thoughts in the comments
Please Login to comment...