flyweight_pattern

namespace flyweight_pattern

Namespace for the Flyweight pattern.

class ConcreteFlyweight : public flyweight_pattern::Flyweight
#include <flyweight.hpp>

ConcreteFlyweight implements the Flyweight interface.

It contains the intrinsic state, which is shared between multiple objects.

Public Functions

inline explicit ConcreteFlyweight(const std::string &state)

Constructor to initialize the intrinsic state.

Parameters:

state – The intrinsic state of the Flyweight.

inline virtual void operation(const std::string &extrinsicState) const override

Implements the Flyweight operation using both intrinsic and extrinsic states.

Parameters:

extrinsicState – The extrinsic state passed by the client.

class Flyweight
#include <flyweight.hpp>

Abstract Flyweight defines the interface for concrete flyweights.

Subclassed by flyweight_pattern::ConcreteFlyweight

Public Functions

virtual void operation(const std::string &extrinsicState) const = 0

Operation performed by the Flyweight.

Parameters:

extrinsicState – The extrinsic state passed by the client.

class FlyweightFactory
#include <flyweight.hpp>

FlyweightFactory manages the Flyweight objects and ensures their reuse.

Public Functions

inline std::shared_ptr<Flyweight> getFlyweight(const std::string &intrinsicState)

Retrieves a Flyweight instance with the given intrinsic state. If no such instance exists, it creates one.

Parameters:

intrinsicState – The intrinsic state of the Flyweight.

Returns:

A shared pointer to the Flyweight instance.

inline void listFlyweights() const

Displays all intrinsic states managed by the factory.