Module design_pattern_examples_rust::flyweight
source · Expand description
The Flyweight design pattern example module
The Flyweight pattern is used when a large object needs to be represented by a much lighter weight struct, possibly multiple instances of said light-weight struct.
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 struct. FlyweightImage structs that contain an 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 using the FlyweightImage struct. The FlyweightImage struct 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.
Accessed through the flyweight_exercise() function.
Modules
- Contains the BigResource struct that holds the multiple images, each of which is represented by the Fylweight struct.
- Contains the BigResourceManager struct that manages the large resource(s) containing big images used by the Flyweight class.
- Contains the FlyweightContext struct that holds the offsets to the Flyweight “image” (in a big resource “image”) along with the position of the Flyweight “image” within a “display”.
- Contains the Display struct that represents the “display” in which to render The Flyweight “images”.
- Contains the FlyweightImage struct that holds the ID of the big resource “image” to draw from and a FlyWeightContext struct that specifies the offsets to the Flyweight along with the position of the Flyweight “image” within a “display”.
Constants
- Height of the “display”, in characters, in which to render Flyweight “images”.
- Width of the “display”, in characters, in which to render Flyweight “images”.
- Height of an individual Flyweight “image”, in characters.
- Width of an individual Flyweight “image”, in characters.
- Number of Flyweight “images” to generate and manipulate.
- Number of iterations of moving and rendering the Flyweight “images”.
Functions
- Clear the given “display” to a background character, erasing whatever was there before.
- 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.
- Helper function to generate the specified number of FlyweightImage struct instances and associate those objects with individual contexts and a single big resource.
- 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 -.
- Move the given flyweight instances within the display, bouncing them off the edges of the display.
- Render the given images into the display, drawing on the big resource “image” found in the given Big Resource Manager.
- Render the display to the screen.
- Generate a display area in which to render the big resource.
- Example of using the “Flyweight” design pattern.