Beginners Guide to Quantum Computing

Quantum Computing seeks to leverage the behaviour of the unorthodox quantum world to give us an advantage over classical computing. This blog contains the resources you can follow to get started with…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Flyweight Design Pattern

Flyweight design pattern comes with under structural design pattern. This pattern is used to reduce the number of objects created and decrease the memory footprint and increase performance.
This flyweight pattern reuse already existing similar kind of objects by storing them. It creates a new object when an unmatching object is found. There is one important feature of the flyweight objects. That flyweight objects are immutable. This means that they cannot be modified once they have been constructed.

When an application uses a number of objects
When the application does not depend on object identity.

Less number of objects reduces the memory of the usage.
It can manage to keep us away from errors related to memory like java.lang.OutOfMemoryError.
We can reduce the execution time of the program by sharing the objects.

In the Flyweight pattern, we use a HashMap that stores references to the object which has already been created, every object is associated with a key. Now when a client wants to create an object, he simply has to pass a key associated with it and if the object has already been created we simply get the reference to that object else it creates a new object and then returns it reference to the client.

let’s consider the following example, Following the example, the Colourfactory class has a HashMap of car objects with different colors and uses a Color object as a key. If the request came to create a Car object with particular color to Colourfactory, It checks that color car object in the HashMap. if that color car object can find in the HashMap it is returned. If it does not exist create a new car object and store it for future use.

If we create the same color car objects you can see that the object hash codes are the same.

Output-

Add a comment

Related posts:

Top IT companies in USA?

Solution Analysts is an eminent mobile app development company with the strong presence in India and the USA. We, at Solution Analysts, offer a spectrum of custom mobile app development services…

Do You Think You Are a Procrastinator?

These statements can come in various forms, but it has only one name — Procrastination. Usually known as a universal troublemaker and sometimes the single biggest productivity killer! Let me take…

Amulet Protocol

Amulet is a new DeFi (decentralized finance) coverage protocol for Rust-based ecosystems. It’s simple, Reliable Cover For Everyone in Web3 Amulet is built with the promise of simple, secure insurance…