Open In App
Related Articles

Difference between Prototype Design Pattern and Flyweight Design Pattern

Improve Article
Improve
Save Article
Save
Like Article
Like

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:

  1. Cost of creating a new object
  2. Reducing complexity
  3. Time-consuming
  4. Avoiding the need for subclassing or inheritance
  5. Flexible object creation runtime
  6. 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 Design Pattern Flyweight Design Pattern
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 31 Oct, 2023
Like Article
Save Article
Previous
Next
Similar Reads