Design Pattern Examples
Overview of object-oriented design patterns
DesignPatternExamples_python.flyweight.flyweight_exercise Namespace Reference

Functions

int _Flyweight_GenerateBigResource (int numImages, int width, int height)
 Generate a big resource, in this case, a text master "image" of the specified height, containing the specified number of smaller images laid out horizontally, using the given width for each image.
 
None _Flyweight_ClearDisplay (list[bytearray] display)
 Clear the "display" to a background image, erasing whatever was there before.
 
list[bytearray] _Flyweight_GenerateDisplay (int width, int height)
 Generate a display area in which to render the big resource.
 
None _Flyweight_ShowDisplay (list[bytearray] display)
 Render the display to the screen.
 
None _Flyweight_MoveFlyweights (list[Flyweight_Class] flyweightInstances, int display_width, int display_height)
 Move the given flyweight instances within the display, bouncing them off the edges of the display.
 
None _Flyweight_RenderFlyweights (list[Flyweight_Class] flyweightInstances, list[bytearray] displayArea)
 Render the image into the display, once for each flyweight instance.
 
float GenerateVelocity ()
 Generate a random velocity, which includes a speed and a direction.
 
list[Flyweight_Class_Flyweight_GenerateFlyweightClasses (int bigResourceId, int numFlyweights, int image_width, int image_height, int display_width, int display_height)
 Helper method to generate the specified number of flyweight class instances and associate those instances with individual contexts and a single big resource.
 
def Flyweight_Exercise ()
 Example of using the Flyweight Pattern.
 

Function Documentation

◆ _Flyweight_ClearDisplay()

None _Flyweight_ClearDisplay ( list[bytearray]  display)
protected

Clear the "display" to a background image, erasing whatever was there before.

Parameters
displayA list of bytearrays representing the display.

Definition at line 56 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_GenerateDisplay(), and DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_GenerateBigResource()

int _Flyweight_GenerateBigResource ( int  numImages,
int  width,
int  height 
)
protected

Generate a big resource, in this case, a text master "image" of the specified height, containing the specified number of smaller images laid out horizontally, using the given width for each image.

If there are 5 images requested, then create a single image that is 5 * width wide and 1 * height tall.

Parameters
numImagesNumber of images to images to store in the single big resource (horizontally), between 1 and 9.
widthWidth of the "text" image, in characters. Minimum width is 3.
heightHeight of the "text" image, in characters. Minimum height is 3.
Returns
An index to the generated index in the BigResourceManager.

Definition at line 29 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_GenerateDisplay()

list[bytearray] _Flyweight_GenerateDisplay ( int  width,
int  height 
)
protected

Generate a display area in which to render the big resource.

Parameters
widthWidth of the display area.
heightHeight of the display area.
Returns
A List of character arrays representing the display area.

Definition at line 70 of file flyweight_exercise.py.

References DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_ClearDisplay().

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_GenerateFlyweightClasses()

list[Flyweight_Class] _Flyweight_GenerateFlyweightClasses ( int  bigResourceId,
int  numFlyweights,
int  image_width,
int  image_height,
int  display_width,
int  display_height 
)
protected

Helper method to generate the specified number of flyweight class instances and associate those instances with individual contexts and a single big resource.

The image and display sizes are provided so as to randomize the position of each flyweight within the display.

Parameters
bigResourceIdID of the big resource to use.`
numFlyweightsNumber of flyweight instances to create.
image_widthWidth of the big resource image.
image_heightHeight of the big resource image.
display_widthWidth of the display in which the flyweight is to be rendered.
display_heightHeight of the display in which the flyweight is to be rendered.
Returns
A list containing the requested number of Flyweight class instances, each attached to the specified "big" resource.

Definition at line 181 of file flyweight_exercise.py.

References DesignPatternExamples_python.flyweight.flyweight_exercise.GenerateVelocity().

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_MoveFlyweights()

None _Flyweight_MoveFlyweights ( list[Flyweight_Class flyweightInstances,
int  display_width,
int  display_height 
)
protected

Move the given flyweight instances within the display, bouncing them off the edges of the display.

The display size and image size are provided here

Parameters
flyweightInstancesList of Flyweight_Class instances to move.
display_widthWidth of display.
display_heightHeight of display.

Definition at line 102 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_RenderFlyweights()

None _Flyweight_RenderFlyweights ( list[Flyweight_Class flyweightInstances,
list[bytearray]  displayArea 
)
protected

Render the image into the display, once for each flyweight instance.

Parameters
flyweightInstancesList of Flyweight_Class instances to render.
displayAreaThe "display" in which to render.

Definition at line 139 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ _Flyweight_ShowDisplay()

None _Flyweight_ShowDisplay ( list[bytearray]  display)
protected

Render the display to the screen.

Parameters
displayA list of bytearrays representing the display.

Definition at line 84 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise.Flyweight_Exercise().

◆ Flyweight_Exercise()

def Flyweight_Exercise ( void  )

Example of using the Flyweight Pattern.

The Flyweight pattern is used when a large object needs to be represented by a much lighter weight class, possibly multiple instances of said light-weight class.

In this example, a large object is represented by a so-called "big resource" (a two-dimensional array of text characters) containing multiple images, one associated with each flyweight class. Flyweight classes that represent offset into the big resource, along with position and velocity, are attached to the big resource image so they all share the same image but have different positions and velocities. The image is rendered to a display area through the Flyweight class. The Flyweight class instances then have their positions updated, bouncing off the edges of the display area 60 times a second. This continues for 1000 iterations or until a key is pressed.

Definition at line 224 of file flyweight_exercise.py.

References DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_ClearDisplay(), DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_GenerateBigResource(), DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_GenerateDisplay(), DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_GenerateFlyweightClasses(), DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_MoveFlyweights(), DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_RenderFlyweights(), and DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_ShowDisplay().

◆ GenerateVelocity()

float GenerateVelocity ( void  )

Generate a random velocity, which includes a speed and a direction.

The velocity is 0.2 to 1.0 (in increments of 0.2) and the direction is either + or -.

Returns
Returns the velocity.

Definition at line 153 of file flyweight_exercise.py.

Referenced by DesignPatternExamples_python.flyweight.flyweight_exercise._Flyweight_GenerateFlyweightClasses().