Design Pattern Examples
Overview of object-oriented design patterns
BigResourceManager Class Reference

Represents a manager for big resources. Also provides the class factory for the Flyweight_Class instances that are associated with a particular big resource. More...

#include <Flyweight_Classes.h>

Collaboration diagram for BigResourceManager:
Collaboration graph

Static Public Member Functions

static int AddResource (std::vector< std::string > rawResource)
 Add a new big resource and return the ID of the resource.
 
static Flyweight_Class::unique_ptr_t CreateFlyweight (int bigResourceId, Flyweight_Context context)
 Create a new instance of the Flyweight_Class associated with the given big resource and a context, in this case an X,Y position.
 

Private Types

using BigResourceList = std::vector< std::unique_ptr< BigResource > >
 

Static Private Member Functions

static int GetNextResourceId ()
 Retrieve the next resource ID that can be used.
 
static BigResourceFindResource (int resourceId)
 Retrieve the BigResource corresponding to the specified ID.
 

Static Private Attributes

static BigResourceList _resources
 A list of all big resources managed by this class.
 
static int _nextResourceId = 1
 The next ID to assign a raw resource for management.
 

Detailed Description

Represents a manager for big resources. Also provides the class factory for the Flyweight_Class instances that are associated with a particular big resource.

Note: The raw resource is provided by the caller and an ID is then associated with it. The ID is returned to the caller who then passes that ID to the Flyweight_Class factory along with a context.

In the exercise, only one big resource is ever created so this class is likely overkill for an example but it fully shows the architectural context in which a flyweight class is utilized.

Definition at line 281 of file Flyweight_Classes.h.

Member Typedef Documentation

◆ BigResourceList

using BigResourceList = std::vector<std::unique_ptr<BigResource> >
private

Definition at line 284 of file Flyweight_Classes.h.

Member Function Documentation

◆ AddResource()

int AddResource ( std::vector< std::string >  rawResource)
static

Add a new big resource and return the ID of the resource.

Parameters
rawResourceThe resource to add.
Returns
Returns the ID of the new big resource added to the manager.

Definition at line 185 of file Flyweight_Classes.cpp.

References BigResourceManager::_resources, and BigResourceManager::GetNextResourceId().

◆ CreateFlyweight()

Flyweight_Class::unique_ptr_t CreateFlyweight ( int  bigResourceId,
Flyweight_Context  context 
)
static

Create a new instance of the Flyweight_Class associated with the given big resource and a context, in this case an X,Y position.

Parameters
bigResourceIdID of the big resource to associate with.
contextA Flyweight_Context instance containing the context for the desired Flyweight_Class instance.
Returns
A new instance of the Flyweight_Class. Returns null if the big resource was not found.

Definition at line 193 of file Flyweight_Classes.cpp.

References BigResource::CreateFlyweight(), and BigResourceManager::FindResource().

◆ FindResource()

BigResource * FindResource ( int  resourceId)
staticprivate

Retrieve the BigResource corresponding to the specified ID.

Parameters
resourceIdID of the big resource to find.
Returns
Returns a BigResource object corresponding to the specified ID. Returns null if there is no corresponding IBigResource object.

Definition at line 167 of file Flyweight_Classes.cpp.

References BigResourceManager::_resources.

Referenced by BigResourceManager::CreateFlyweight().

◆ GetNextResourceId()

int GetNextResourceId ( )
staticprivate

Retrieve the next resource ID that can be used.

Returns
The next resource ID.

Definition at line 160 of file Flyweight_Classes.cpp.

References BigResourceManager::_nextResourceId.

Referenced by BigResourceManager::AddResource().

Member Data Documentation

◆ _nextResourceId

int _nextResourceId = 1
staticprivate

The next ID to assign a raw resource for management.

Definition at line 294 of file Flyweight_Classes.h.

Referenced by BigResourceManager::GetNextResourceId().

◆ _resources

BigResourceManager::BigResourceList _resources
staticprivate

A list of all big resources managed by this class.

Definition at line 289 of file Flyweight_Classes.h.

Referenced by BigResourceManager::AddResource(), and BigResourceManager::FindResource().


The documentation for this class was generated from the following files: