What is the Flyweight design pattern and why we use ?
The topic is Flyweight design pattern in programming language flyweight is a software design pattern. A flyweight is an object that minimizes memory usage by sharing as much data as possible with other similar object.
it is way to use objects in large numbers when simple Repeated representation would use an unacceptable amount of memory. therefore it is devided into two parts shown in flyweight design pattern class diagram . if multiple objects have some part is comman means feature or any internal part is same then this object share these memory with the help of that we can reduced memory consumption. and the same part of different objects is stored and shared via a Flyweight object.The common part of various object is stored and shared via a
“Flyweight” object.
Flyweight Factory : it is a factory class which is used by client class to get data from the concrete_flyweight . this class is also responsible for creating a pool of already created objects.so when a request comes from data that is already requested, it returns data from pool.
Flyweight Design Pattern Example :
In this example we take a Aeroplane industry model means each Aeroplane information is stored in the form of class objects. so each have same entity like type of aircraft , seating capacity,speed etc.
these information stored in two different ways because some entities are same and other is unique.
so same information can be stored at one place in flyweight objects which will be shared by all same kind of objects. and other information like every aircraft will have serial number and manufacturing date information which are unique per aircraft that's why ,these information will be saved directly in final object.
Code:
#include<iostream>
using namespace std;
class Product
{
private:
string p_name;
int p_capacity;
int p_speed;
public:
virtual void showDetails()
{
cout<<"Name : "<<p_name<<", Capacity : "<<p_capacity<<", Speed: "<<p_speed<<" knots" <<endl;
}
Product(string name, int capacity, int speed)
{
p_name = name;
p_capacity = capacity;
p_speed = speed;
}
Product(){};
};
/* Flyweight Concrete Class */
class Boeing380: public Product
{
public:
Boeing380():Product("Boeing380", 200, 800) {}
};
/* Flyweight Concrete Class */
class Airbus787: public Product
{
public:
Airbus787():Product("Airbus787", 600, 1000) {}
};
/* Flyweight Concrete Class */
class Airbus797: public Product
{
public:
Airbus797():Product("Airbus797", 1200, 1500) {}
};
/* Flyweight Factory Class */
class FlyweightFact
{
private:
static Boeing380* s_boeing380;
static Airbus787* s_airbus787;
static Airbus797* s_airbus797;
public:
static Product* getProduct(int type)
{
switch(type)
{
case 380:
if(!s_boeing380)
{
s_boeing380 = new Boeing380();
}
return s_boeing380;
case 787:
if(!s_airbus787)
{
s_airbus787 = new Airbus787();
}
return s_airbus787;
case 797:
if(!s_airbus797)
{
s_airbus797 = new Airbus797();
}
return s_airbus797;
default:
cout<<"Unknown aeroplane type"<<endl;
return NULL;
}
}
};
Boeing380* FlyweightFact::s_boeing380;
Airbus787* FlyweightFact::s_airbus787;
Airbus797* FlyweightFact::s_airbus797;
/* Product Class */
class Aeroplane: public Product
{
private:
Product* p_product;
string p_mfgDate;
int p_id;
public:
Aeroplane(Product* product, string date, int id)
{
p_product = product;
p_mfgDate = date;
p_id = id;
}
virtual void showDetails()
{
p_product->showDetails();
cout<<"MfgDate : "<<p_mfgDate<<", Serial No: "<<p_id<<endl;
}
};
int main()
{
Aeroplane first = Aeroplane(FlyweightFact::getProduct(787), "10th May 1997", 100213);
Aeroplane second = Aeroplane(FlyweightFact::getProduct(797), "1st Aug 1998", 100214);
Aeroplane third = Aeroplane(FlyweightFact::getProduct(787), "20th Aug 2000", 100215);
Aeroplane forth = Aeroplane(FlyweightFact::getProduct(380), "10th Jan 2002", 200216);
first.showDetails();
second.showDetails();
third.showDetails();
forth.showDetails();
}
Output :