Flyweight Design Pattern in Java

Posted By : Akash Goyal | 23-May-2018

According to flyweight design pattern intent is: Utilize sharing to help extensive quantities of fine-grained protests proficiently.Flyweight design pattern is the example of structural design pattern.

 

Flyweight design pattern is utilized when we have to make a great deal of Objects of a class. Since each question devours memory space that can be pivotal for low memory gadgets, for example, cell phones or implanted frameworks, flyweight configuration example can be connected to lessen the heap on memory by sharing object.

 

Before we apply flyweight design pattern, we have to think about after variables:

 

  • The quantity of Objects to be made by application ought to be colossal. 
  • The question creation is substantial on memory and it can be tedious as well.
  • The question properties can be isolated into inherent and extraneous properties, extrinsic properties of an Object should be defined by the client program.

 

To apply flyweight design pattern, we have to separate Object property into intrinsic and extrinsic properties. Characteristic properties make the Object one of a kind while outward properties are set by customer code and used to perform distinctive activities. For instance, an Object Circle can have extraneous properties, for example, shading and width.


 

For applying flyweight design, we have to make a Flyweight factory that restores the mutual articles. For our illustration, lets say we have to make an illustration with lines and Ovals. So we will have an interface Shapeand its solid executions as Line and Oval. Oval class will have natural property to decide if to fill the Oval with given shading or not though Line won't have any characteristic property.

 

Flyweight Design Pattern Example in JDK

 

All the wrapper classes valueOf() method utilizes reserved items indicating utilization of Flyweight configuration design. The best illustration is Java String class String Pool implementation.

 

Flyweight Design Pattern Important Points

 

1. In our illustration, the customer code isn't compelled to make question utilizing Flyweight industrial facility however we can constrain that to ensure customer code utilizes flyweight design execution yet its a total plan choice for the specific application.

 

2. Flyweight design presents intricacy and if the number of shared items are colossal at that point there is an exchange of amongst memory and time, so we have to utilize it reasonably in view of our necessities.

 

3. Flyweight design execution isn't helpful when the quantity of characteristic properties of Object is immense, influencing usage of Factory to class complex.

That is just for Flyweight design pattern in java.

 

 

 

About Author

Author Image
Akash Goyal

Akash is a software engineer and good working experience in Java, rest and soap API, SQL, data structure and algorithm and learns new technologies. His hobbies are solving a puzzle and playing chess etc.

Request for Proposal

Name is required

Comment is required

Sending message..