Flyweight Pattern – Structural Design Pattern

Flyweight pattern comes under Structural Design Patterns. By using this pattern we can decrease object count. When there is a need to create huge amount of similar objects then this Java Flyweight Pattern is useful.

Flyweight pattern supports factory pattern which tells to reuse of available objects, where objects are created and stored. Whenever an object is requested, instead of creating new one, factory checks its existence, if it is not present, new object is created otherwise existing one is returned. There are 2 types of attributes for Flyweight objects:

  1. Intrinsic: This kind of attribute is shared or stored in Flyweight object, and this attribute is independent of flyweight’s context. The best practice is make this state immutable.
  2. Extrinsic: This kind of attribute varies with flyweight’s context that’s the reason they cannot be shared.

Flyweight Pattern by Example

We will demonstrate Flyweight Pattern by using Tea ordering Example.

For Tea, suppose only 3 flavors are available 1. Green Tea 2. Regular Tea   3. Ginger Tea. So only 3 tea objects are created.

Now whenever new tea object is requested, factory pattern will not create new one it will check for its existence, if the object is present, factory will return that object otherwise new object gets created.

Suppose we are serving 10 cups of tea to different customers but we have only 3 flavors (objects).

So if customer is requesting for Green Tea, factory pattern will check whether Green Tea object is created or not. If it is already created, factory pattern will reuse that object.(it will not create new object).

Lets define simple interface.

Also we will add a context to keep table number stored somewhere.

Below is our Factory. The Factory class will check for available objects based on flavor and depending upon its existence, it will either create or return whatever is available.

The we will add concrete implementation of interface to actually serve tea.

Running the example

For demo, we will keep the orders limited to 20 only and show some object creation. Check below class.

Output:

Hope this small example is clear enough to understand Flyweight Pattern

Conclusion

Flyweight design pattern is very useful in an application which needs same kind of objects in repetition. This pattern consumes less memory because objects get shared so we can say that by using this pattern we can reduce the load on memory.

Similar to Flyweight Pattern, if you wish to have a look at other Java Design Patterns, then check this out. You can also find more sample in another languages at Wiki Page

Add a Comment

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