Flyweight Pattern in C++

2019-06-04 06:54发布

When creating a flyweight pattern, how does one usually store superflous objects. Lets say I create an Aircraft.

class Aircraft
{
private:
    char make[32];
    char model[32];
    double cruiseSpeed;
    double cruiseAltitude;
    double climbRate;
    double wingspan;
    double fuselageLength;

    AircraftType craftType;

public:

    Aircraft(void);
    Aircraft(AircraftType);
    ~Aircraft(void);

    void setMake(char*);
    void setModel(char*);
    void setCruiseSpeed(double);
    void setCruiseAltitude(double);
    void setClimbRate(double);
    void setWingSpan(double);
    void setFuselageLength(double);

    char* getMake();
    char* getModel();
    double getCruiseSpeed();
    double getCruiseAltitude();
    double getClimbRate();
    double getWingSpan();
    double getFuselageLength();    
};

Lets also say I'm reading in many aircrafts from an xml data sheet, so I want to just create one one these objects and store the rest of the information somewhere. Would I create another class that manages the flyweight information, then maybe pass it a key with the Aircraft object to input into the only instance one at a time? Or would you just bypass putting the information in the object instance at all?

For this type of object, would the only efficiency gained in not creating multiple instance be the get and sets?

1条回答
再贱就再见
2楼-- · 2019-06-04 07:18

Look at the boost flyweight implementation. After you do, just use boost flyweight.

If you don't want to use boost, use boost in such case.

查看更多
登录 后发表回答