Flyweight Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

This article will deal with not so commonly heard i.e. flyweight design pattern implementation. This is used when objects have some state that is internal to them and shared across object and some state which is given from the client and it is not shared.

 

Flyweight Design Pattern Implementation:

Definition:

Flyweight design pattern uses sharing to support large numbers of objects that have part of their internal state common and other parts of state can vary.

Each flyweight object have two states

  • Intrinsic state: This is internal to flyweight object and shared across it. Usually represented in the form of member variables.
  • Extrinsic state: This state comes from client in the form of method parameters. It is not shared across the object.

Generic Class Diagram:

Flyweight Class Diagram

Flyweight Class Diagram

Participants involved:

  • Flyweight: An interface which defines methods which can take extrinsic state from client.
  • ConcreteFlyweight:
    • It should be able to share intrinsic state which can be shared across that object.
    • Concrete implementation should be able to perform operations on extrinsic state.
  • FlyweightFactory:
    • Creates and manages flyweight objects.
    • Depending on requirement, it can maintain pool of objects or single objects.
    • It should be able to return flyweight objects to caller.
  • Client: Maintains reference to Flyweight and FlyweightFactory and provides extrinsic state

Flyweight Design Pattern Implementation Example:

Consider that you are a biker and now want to ride your bike with some specific speed. what do we do? We retrieve our bike, hop on it and ride at any speed you want.

  • We have a Flyweight interface i.e. Bike which declares method rideBike(int speed); speed comes from client and hence it is extrinsic state.
  • SportsBike implements Bike, its is of type SportsBike, which is not going to change. So we define type=”SportsBike”. It is intrinsic state.
  • BikeFactory: Simply instantiates a bike and returns previously created bike if already present.

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout flyweight

If you don’t have git installed, you can either directly download zip or copy paste from below.

Bike.java:

SportsBike.java:

BikeFactory.java:

Now we just need to retrieve the bike and ride it.

Client.java:

Output:

Note that “SportsBike” is intrinsic state which is shared in object and used in rideBike() method and speed is extrinsic state provided by client.

Flyweight Pattern Example Class Diagram:

Flyweight Example Diagram

Flyweight Example Diagram

I hope the article helped understand Flyweight Design Pattern Implementation.

Share Button

2 comments for “Flyweight Design Pattern Implementation

  1. \
    October 20, 2016 at 7:01 pm

    This is factory pattern not flyweight. OMFG

    • October 21, 2016 at 9:14 am

      Factory pattern is used in conjunction with flyweight. You can refer multiple blogs and websites over internet for checking the same.

Leave a Reply

Your email address will not be published. Required fields are marked *