7using System.Collections.Generic;
54 else if (numImages > 9)
67 List<string> image =
new List<string>();
68 for (
int row = 0; row < height; ++row)
70 string image_row =
"";
71 for (
int imageIndex = 0; imageIndex < numImages; imageIndex++)
73 if (row == 0 || (row + 1) == height)
76 image_row +=
"+" +
new string(
'-', width - 2) +
"+";
83 char c = imageIndex.ToString()[0];
84 image_row +=
'|' +
new string(c, width - 2) +
'|';
103 List<char[]> display =
new List<char[]>();
105 for (
int row = 0; row < height; ++row)
107 display.Add(
new char[width]);
121 StringBuilder output =
new StringBuilder();
122 foreach (
char[] row
in display)
125 foreach (
char col
in row)
129 output.Append(Environment.NewLine);
131 Console.WriteLine(output);
141 foreach (
char[] row
in display)
143 for (
int index = 0; index < row.Length; ++index)
167 double newx = context.Position_X + context.
Velocity_X;
168 double newy = context.Position_Y + context.
Velocity_Y;
170 if (newx < 0 || (newx + image_width) > display_width)
180 newx = display_width - image_width;
184 if (newy < 0 || (newy + image_height) > display_height)
194 newy = display_height - image_height;
198 context.Position_X = newx;
199 context.Position_Y = newy;
200 flyweight.Context = context;
216 flyweight.
Render(displayArea,
233 double speed = (randomizer.Next(1, 5) / 5.0);
234 double direction = ((randomizer.Next(100) > 50) ? 1.0 : -1.0);
235 return speed * direction;
254 int image_width,
int image_height,
int display_width,
int display_height)
256 List<Flyweight_Class> flyweightInstances =
new List<Flyweight_Class>();
258 Random randomizer =
new Random();
261 for (
int index = 0; index < numFlyweights; ++index)
264 context.OffsetXToImage = index * image_width;
265 context.ImageWidth = image_width;
266 context.ImageHeight = image_height;
268 context.Position_X = randomizer.Next(0, display_width - image_width);
269 context.Position_Y = randomizer.Next(0, display_height - image_height);
277 if (flyweightInstance !=
null)
279 flyweightInstances.Add(flyweightInstance);
283 Console.WriteLine(
" Error! Failed to find big resource ID {}, which is needed for the flyweight.", bigResourceId);
286 return flyweightInstances;
296 Console.WriteLine(
"Flyweight Exercise");
299 const int DISPLAY_WIDTH = 80;
300 const int DISPLAY_HEIGHT = 20;
301 const int IMAGE_WIDTH = 30;
302 const int IMAGE_HEIGHT = 5;
303 const int NUMFLYWEIGHTS = 5;
304 const int NUM_ITERATIONS = 1000;
307 List<Flyweight_Class> flyweightInstances;
309 IMAGE_WIDTH, IMAGE_HEIGHT, DISPLAY_WIDTH, DISPLAY_HEIGHT);
318 Console.WriteLine(
" The image rendered {0} times:", NUMFLYWEIGHTS);
319 Console.WriteLine(
"");
325 int cursorLeft = Console.CursorLeft;
326 int cursorTop = Console.CursorTop;
327 cursorTop -= DISPLAY_HEIGHT + 1;
328 for (
int index = 0; index < NUM_ITERATIONS; ++index)
330 Console.SetCursorPosition(cursorLeft, cursorTop - 1);
331 Console.WriteLine(
" {0,5}/{1} iterations [press a key to exit early]", index + 1, NUM_ITERATIONS);
332 Console.SetCursorPosition(cursorLeft, cursorTop);
338 System.Threading.Thread.Sleep(16);
339 if (Console.KeyAvailable)
345 Console.WriteLine(
" Done.");
static double GenerateVelocity(void)
Generate a random velocity, which includes a speed and a direction. The velocity is 0....
Represents a manager for big resources. Also provides the class factory for the Flyweight_Class insta...
static int AddResource(List< string > rawResource)
Add a new big resource and return the ID of the resource.
static ? Flyweight_Class CreateFlyweight(int bigResourceId, Flyweight_Context context)
Create a new instance of the Flyweight_Class associated with the given big resource and a context,...
Associates a context with a big resource.
int ImageHeight
Retrieve the "image" height from underlying big resource.
int ImageWidth
Retrieve the "image" width from underlying big resource.
void Render(List< 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...
Flyweight_Context Context
Retrieve or set the context for this class instance.
Example of using the Flyweight Pattern in C#.
void _Flyweight_RenderFlyweights(List< Flyweight_Class > flyweightInstances, List< char[]> displayArea)
Render the image into the display, once for each flyweight instance.
int _Flyweight_GenerateBigResource(int numImages, int width, int height)
Generate a big resource, in this case, a text master "image" of the specified height,...
double GenerateVelocity(Random randomizer)
Generate a random velocity, which includes a speed and a direction. The velocity is 0....
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.
List< char[]> _Flyweight_GenerateDisplay(int width, int height)
Generate a display area in which to render the big resource.
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 insta...
void Run()
Executes the example for the Flyweight Pattern in C#.
void _Flyweight_ClearDisplay(List< char[]> display)
Clear the "display" to a background image, erasing whatever was there before.
void _Flyweight_ShowDisplay(List< char[]> display)
Render the display to the screen.
The namespace containing all Design Pattern Examples implemented in C#.
Represents the context for an instance of the Flyweight_Class. In this case, the context includes pos...