Javatpoint Logo
Javatpoint Logo

Flyweight Design Pattern

Like facade, adapter, and decorator patterns, flyweight design patterns are structural patterns. The flyweight design pattern is applied when producing many class objects. By sharing objects, the flyweight design pattern helps lessen the strain on memory, which is important for low memory devices like embedded systems and mobile devices. This is because every object takes up memory space. Prior to using the flyweight design pattern, the following elements must be considered:

  • The program should generate many objects.
  • The process of creating an object takes a lot of memory and might take some time.
  • Extrinsic attributes of an item should be determined by the client program. The properties of an object can be classified as intrinsic or extrinsic.

We must separate Object attribute into intrinsic and extrinsic qualities to apply the flyweight pattern. While extrinsic characteristics are set by client code and are utilised for various purposes, intrinsic properties are what give an object its uniqueness. An Object Circle, for instance, may possess external characteristics like colour and breadth. We must build a Flyweight factory that yields the shared objects to apply the flyweight pattern. Let us take an example where we need to draw something using lines and ovals. Thus, we shall own an interface Shape together with its tangible realisations, which are Line and Oval. Whereas Line has no intrinsic property, oval class will have one to decide whether to fill the oval with the specified colour or not.

Line.java

Oval.java

Flyweight Factory

We need to maintain a map of objects in the flyweight factory that client applications should not be able to access, as the flyweight factory will be utilised by client programs to instantiate the object. When a client program calls the HashMap to retrieve an instance of an object, the object should always be returned; if it cannot be located, a new object should be created, added to the Map, and then returned. When generating the Object, we must ensure that all the inherent attributes are considered. The code for our flyweight factory class is as follows.

Take note of how the getShape function uses the Factory design, Java Composition (a shapes map), and Java Enum for type safety.

Flyweight Design Pattern Client Example

An example programme that uses flyweight pattern implementation is shown below. Java DrawingClient.

DrawingClient.java

Output:

Creating Line object
Creating Oval object with fill=true
Creating Oval object with fill=false

I created several kinds of shapes for our frame using random number generator. You may notice a delay in the creation of the first Line Object and Oval objects with fill set to true and false if you execute the above client program. After that, because it is using shared objects, the program runs rapidly. Once you click the "Draw" button several times, the frame should resemble the image below.

Flyweight Design Pattern

That concludes our discussion of flyweight patterns; in next posts, we will examine other design patterns. If you like it, please let us know in the comments area and spread the word.

Output:

Counter Terrorist Created 
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB 
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB 
Terrorist Created 
Terrorist with weapon AK-47| Task is PLANT A BOMB 
Terrorist with weapon Gut Knife| Task is PLANT A BOMB 
Terrorist with weapon Gut Knife| Task is PLANT A BOMB 
Terrorist with weapon Desert Eagle| Task is PLANT A BOMB 
Terrorist with weapon AK-47| Task is PLANT A BOMB 
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB 
Counter Terrorist with weapon Gut Knife| Task is DIFFUSE BOMB 
Counter Terrorist with weapon Desert Eagle| Task is DIFFUSE BOMB






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA