- Flyweight is a software design pattern.
- Make instances of classes on the fly to improve
- A flyweight is an object that minimizes memory use
as much data as possible with other similar objects;
- It is a way to use objects in large numbers when a
representation would use an unacceptable amount of memory.
- The term comes from the boxing weight class with the
- Use sharing to support large numbers of fine-grained
examples of Flyweight patterns
- An application uses a large number of objects.
- Storage costs are high because of the sheer quantity
- Most object state can be made extrinsic.
- Many groups of objects may be replaced by relatively
few shared objects once
extrinsic state is removed.
- The application doesn't depend on object identity.
Since flyweight objects may
be shared, identity tests will return true for conceptually distinct
- The Flyweight uses sharing to support large numbers
of objects efficiently.
- The public switched telephone network is an
example of a Flyweight.
- There are several resources such as dial
tone generators, ringing generators, and digit
receivers that must be shared between all subscribers.
- A subscriber is unaware of how many
resources are in the pool when he or
she lifts the handset to make a call.
- All that matters to subscribers is that a
dial tone is provided, digits are
received, and the call is completed
public abstract class Admission
public abstract void admitStudent
public class AdmissionContext
public AdmissionContext(int std)
this.std = std;
public int getStd()
public class GeneralAdmission extends Admission
String admissionType = null;
public GeneralAdmission(String admissionType)
this.admissionType = admissionType;
public String getAdmissionType()
public void admitStudent(AdmissionContext admissionContext)
System.out.println("Admissions type =
"+admissionType+" for std "+admissionContext.getStd());