Structural Patterns - Flyweight Pattern

The pattern here states about a mechanism by which you can avoid creating a large number of object instances to represent the entire system.

To decide if some part of your program is a candidate for using Flyweights, consider whether it is possible to remove some data from the class and make it extrinsic. If this makes it possible to reduce greatly the number of different class instances your program needs to maintain, this might be a case where Flyweights will help.

The typical example you can see on this in every book will be of folders. The folder with name of each of the company employee on it, so, the attributes of class Folder are: ‘Selected’ , ‘Not Selected’ and the third one is ‘employeeName’. With this methodology, we will have to create 2000 folder class instances for each of the employees. This can be costly, so we can create just two class instances with attributes ‘selected’ and ‘not selected’ and set the employee’s name by a method like:

setNameOnFolder(String name);

This way, the instances of class folder will be shared and you will not have to create multiple instances for each employee.

I was going through this pattern and was trying to find the best suited non-software example. Then, I remembered the talks I had with one of my cousin’s who used to work in a grinding wheel manufacturing company. I am a Chemical Engineer and so, remember the names of chemical compounds. He was telling me that the grinding wheels are used for metal cutting across the industry. Basically the main ingredients for these grinding wheels are Aluminum Oxide (Al2O3) and Silicon Carbide (SiC). These compounds are used in form of grains. For those who remember Chemistry from schools, and for others, just follow the example.

His company manufactures nearly 25000 types of grinding wheels. Now, there is another technicality in this and that is bonding.

There are two types of bondings used to bond the material i.e. Aluminum Oxide and Silicon Carbide together. One is Glass bonding – this is like, the wheel is heated to 1300 degree C and the silicon turns into glass to hold the two materials together. The second type of bonding is Resin bonding, this is when some resins help in holding the materials together. The wheels are in different sizes, have different ratio of materials mixed and have any of these two types of bondings. This decides the strength of the wheel. In all, creating 25,000 types of combinations is a pretty complex looking scenario.

If we consider this from software point of view, we can see that each wheel is of a different type and so, we need to make 25000 classes for taking care of each of the wheel. This of course will be very resource intensive. So, how to avoid this?

Well, we already know a few things and we can see a common pattern in each of the wheels. The common things are as follows:

1. Materials of use – They are always Aluminum Oxide and Silicon Carbide.
2. Each wheel has a bonding.
3. Each wheel has a size.

We can follow one thing, the combination above mentioned three ingredients can give us a large number of instances so, why not take a scenario where only one of these is used in the constructor and rest be passed as method parameters.

Let’s have a look at the code. For every flyweight, there is a factory providing the object instance. Now, naturally wheels are made in a factory so, there is GrindingWheelFactory class which returns the type of wheel needed.

package structural.flyweight;

* This factory of wheel is accessed by the client. Everytime,
* the client wants a wheel, the software is accessed through this
* Factory class. The specifications are passed through the
* method parameters and a new wheel is returned.
* User: prashant.satarkar
* Date: Apr 8, 2004
* Time: 5:06:13 PM
public class GrindingWheelFactory {

* The method takes the input parameters and return the appropriate
* wheel.
* @return An instance of grinding wheel
public GrindingWheel getWheel(boolean isGlassBonded) {

return new GrindingWheel(isGlassBonded);

}// End of interface

This class is very important. Let’s have a closer look at the class. It returns a wheel based only on the bonding. As we know that bondings are only of two types, and so, at any point in time, there will be two instances which are negligible as compared to 25000.

The other important class here is of course GrindingWheel. This gets constructed depending on the parameters passed to the method getWheel() of class GrindingWheelFactory. Let’s have a look at the class GrindingWheel.

package structural.flyweight;

* The wheel is formed for different ratio of alumina
* and silicon carbide, for a different bonding and for
* a different size, which depends on the diameter of
* the wheel.
* User: prashant.satarkar
* Date: Apr 8, 2004
* Time: 5:13:23 PM
public class GrindingWheel {

  private int ratioAlumina;
private int diameter;
private boolean isGlassBonded;

* Default Constructor
public GrindingWheel(boolean isGlassBonded) {
this. isGlassBonded = isGlassBonded;

}// End of class

This class can have other methods getters and setters for diameter, and ratioAlumina, on which the complete wheel is dependent.

In each of the instances of the wheels, we can pass the values of ratio of alumina to silicon carbide as method parameters and also the sizes which can lead to a great number of combinations.

Hence, we can see that by using the flyweight pattern, we can reduce the instances of the class.

Creational Patterns
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Builder Pattern
Prototype Pattern
Structural Patterns
Adapter Pattern
Bridge Pattern
Composite Pattern
Decorator Pattern
Facade Pattern
Flyweight Pattern
Proxy Pattern
Behavioral Patterns
Chain of Responsibility Pattern
Command Pattern
Interpreter Pattern
Iterator Pattern
Mediator Pattern
Momento Pattern
Observer Pattern

State Pattern
Strategy Pattern
Template Pattern

Visitor Pattern
Use of this website constitutes acceptance of the Terms and Conditions