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

Associates a context with a big resource. More...

#include <Flyweight_Classes.h>

Collaboration diagram for Flyweight_Class:
Collaboration graph

Public Types

using unique_ptr_t = std::unique_ptr< Flyweight_Class >
 

Public Member Functions

 Flyweight_Class ()
 Default constructor.
 
 Flyweight_Class (BigResource *resource, Flyweight_Context context)
 Constructor (accessible only to the class factory).
 
 ~Flyweight_Class ()
 Destructor.
 
Flyweight_Context Context ()
 Retrieve the context for this class instance.
 
void SetContext (Flyweight_Context context)
 Set the context for this class instance.
 
int ImageWidth ()
 Retrieve the "image" width from underlying big resource.
 
int ImageHeight ()
 Retrieve the "image" height from underlying big resource.
 
void Render (std::vector< std::vector< char > > &display, int offset_x, int image_width, int image_height, int position_x, int position_y)
 Render the image associated with this flyweight instance into the given display at the given position.
 

Private Attributes

BigResource_resource
 The big resource being referenced by this flyweight class. In C#, a class is always a reference to an instance of the class so we take advantage of that here. In other languages, this would be a pointer to the big resource or a handle from which a pointer could be obtained.
 
Flyweight_Context _context
 The context associated with this class. The calling entity uses this context to manipulate the flyweight class.
 

Detailed Description

Associates a context with a big resource.

Definition at line 86 of file Flyweight_Classes.h.

Member Typedef Documentation

◆ unique_ptr_t

using unique_ptr_t = std::unique_ptr<Flyweight_Class>

Definition at line 89 of file Flyweight_Classes.h.

Constructor & Destructor Documentation

◆ Flyweight_Class() [1/2]

Flyweight_Class ( )
inline

Default constructor.

Definition at line 111 of file Flyweight_Classes.h.

◆ Flyweight_Class() [2/2]

Flyweight_Class ( BigResource resource,
Flyweight_Context  context 
)

Constructor (accessible only to the class factory).

Parameters
resourceThe resource being represented by this class. This takes advantage of the fact that C# classes are always passed by reference so no actual copying of the resource is done.
contextA Flyweight_Context instance containing the context for this Flyweight_Class instance.

Definition at line 16 of file Flyweight_Classes.cpp.

References Flyweight_Class::_context, and Flyweight_Class::_resource.

◆ ~Flyweight_Class()

Destructor.

Definition at line 22 of file Flyweight_Classes.cpp.

Member Function Documentation

◆ Context()

Flyweight_Context Context ( )

Retrieve the context for this class instance.

Definition at line 26 of file Flyweight_Classes.cpp.

References Flyweight_Class::_context.

◆ ImageHeight()

int ImageHeight ( )

Retrieve the "image" height from underlying big resource.

Definition at line 47 of file Flyweight_Classes.cpp.

References Flyweight_Class::_context, Flyweight_Class::_resource, and Flyweight_Context::ImageHeight.

◆ ImageWidth()

int ImageWidth ( )

Retrieve the "image" width from underlying big resource.

Definition at line 37 of file Flyweight_Classes.cpp.

References Flyweight_Class::_context, Flyweight_Class::_resource, and Flyweight_Context::ImageWidth.

◆ Render()

void Render ( std::vector< std::vector< char > > &  display,
int  offset_x,
int  image_width,
int  image_height,
int  position_x,
int  position_y 
)

Render the image associated with this flyweight instance into the given display at the given position.

Parameters
displayA list of character arrays representing the display.
offset_xOffset from left edge of big resource to the left edge of the image to render.
image_widthWidth of image to render.
image_heightHeight of image to render.
position_xleftmost position within the display to place the upper left corner of the image,
position_ytopmost position within the display to place the upper left corner of the image.

Note that although this class has access to the context, it really shouldn't so as to keep a loose coupling between the context and this class. Therefore, the entity calling Render() will pass the position in as parameters even though that position is likely coming from the context.

Definition at line 56 of file Flyweight_Classes.cpp.

References Flyweight_Class::_resource, and BigResource::Render().

◆ SetContext()

void SetContext ( Flyweight_Context  context)

Set the context for this class instance.

Definition at line 31 of file Flyweight_Classes.cpp.

References Flyweight_Class::_context.

Member Data Documentation

◆ _context

Flyweight_Context _context
private

The context associated with this class. The calling entity uses this context to manipulate the flyweight class.

Definition at line 105 of file Flyweight_Classes.h.

Referenced by Flyweight_Class::Context(), Flyweight_Class::Flyweight_Class(), Flyweight_Class::ImageHeight(), Flyweight_Class::ImageWidth(), and Flyweight_Class::SetContext().

◆ _resource

BigResource* _resource
private

The big resource being referenced by this flyweight class. In C#, a class is always a reference to an instance of the class so we take advantage of that here. In other languages, this would be a pointer to the big resource or a handle from which a pointer could be obtained.

Definition at line 99 of file Flyweight_Classes.h.

Referenced by Flyweight_Class::Flyweight_Class(), Flyweight_Class::ImageHeight(), BigResource::ImageHeight(), Flyweight_Class::ImageWidth(), BigResource::ImageWidth(), BigResource::Render(), and Flyweight_Class::Render().


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