Example of using the Flyweight Pattern in C#. More...
Public Member Functions | |
void | Run () |
Executes the example for the Flyweight Pattern in C#. | |
Private Member 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. | |
List< char[]> | _Flyweight_GenerateDisplay (int width, int height) |
Generate a display area in which to render the big resource. | |
void | _Flyweight_ShowDisplay (List< char[]> display) |
Render the display to the screen. | |
void | _Flyweight_ClearDisplay (List< char[]> display) |
Clear the "display" to a background image, erasing whatever was there before. | |
void | _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. | |
void | _Flyweight_RenderFlyweights (List< Flyweight_Class > flyweightInstances, List< char[]> displayArea) |
Render the image into the display, once for each flyweight instance. | |
double | GenerateVelocity (Random randomizer) |
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 -. | |
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. | |
Example of using the Flyweight Pattern in C#.
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 31 of file Flyweight_Exercise.cs.
|
inlineprivate |
Clear the "display" to a background image, erasing whatever was there before.
display | A list of character arrays representing the display. |
Definition at line 139 of file Flyweight_Exercise.cs.
Referenced by Flyweight_Exercise._Flyweight_GenerateDisplay(), and Flyweight_Exercise.Run().
|
inlineprivate |
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.
numImages | Number of images to images to store in the single big resource (horizontally), between 1 and 9. |
width | Width of each "text" image, in characters. Minimum width is 3. |
height | Height of each "text" image, in characters. Minimum height is 3. |
Definition at line 48 of file Flyweight_Exercise.cs.
References BigResourceManager.AddResource().
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
Generate a display area in which to render the big resource.
width | Width of the display area. |
height | Height of the display area. |
Definition at line 101 of file Flyweight_Exercise.cs.
References Flyweight_Exercise._Flyweight_ClearDisplay().
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
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.
bigResourceId | ID of the big resource to use. |
numFlyweights | Number of flyweight instances to create. |
image_width | Width of the big resource image. |
image_height | Height of the big resource image. |
display_width | Width of the display in which the flyweight is to be rendered. |
display_height | Height of the display in which the flyweight is to be rendered. |
Definition at line 253 of file Flyweight_Exercise.cs.
References BigResourceManager.CreateFlyweight(), and GenerateVelocity().
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
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
flyweightInstances | List of Flyweight_Class instances to move. |
display_width | Width of display. |
display_height | Height of display. |
Definition at line 160 of file Flyweight_Exercise.cs.
References Flyweight_Class.Context, Flyweight_Class.ImageHeight, Flyweight_Class.ImageWidth, Flyweight_Context.Velocity_X, and Flyweight_Context.Velocity_Y.
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
Render the image into the display, once for each flyweight instance.
flyweightInstances | List of Flyweight_Class instances to render. |
displayArea | The "display" in which to render. |
Definition at line 210 of file Flyweight_Exercise.cs.
References Flyweight_Class.Context, Flyweight_Class.ImageHeight, Flyweight_Class.ImageWidth, Flyweight_Context.OffsetXToImage, Flyweight_Context.Position_X, Flyweight_Context.Position_Y, and Flyweight_Class.Render().
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
Render the display to the screen.
display | A list of character arrays representing the display. |
Definition at line 119 of file Flyweight_Exercise.cs.
Referenced by Flyweight_Exercise.Run().
|
inlineprivate |
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 -.
Definition at line 231 of file Flyweight_Exercise.cs.
|
inline |
Executes the example for the Flyweight Pattern in C#.
Definition at line 293 of file Flyweight_Exercise.cs.
References Flyweight_Exercise._Flyweight_ClearDisplay(), Flyweight_Exercise._Flyweight_GenerateBigResource(), Flyweight_Exercise._Flyweight_GenerateDisplay(), Flyweight_Exercise._Flyweight_GenerateFlyweightClasses(), Flyweight_Exercise._Flyweight_MoveFlyweights(), Flyweight_Exercise._Flyweight_RenderFlyweights(), and Flyweight_Exercise._Flyweight_ShowDisplay().
Referenced by Program.Run().