Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_Class.cs
Go to the documentation of this file.
1
8
9using System.Collections.Generic;
10
12{
21 public struct Flyweight_Context
22 {
23 public int OffsetXToImage;
24 public int ImageWidth;
25 public int ImageHeight;
26 public double Position_X;
27 public double Position_Y;
28 public double Velocity_X;
29 public double Velocity_Y;
30
42 public Flyweight_Context(int offset_x, int image_width, int image_height, double position_x, double position_y, double velocity_x, double velocity_y)
43 {
44 OffsetXToImage = offset_x;
45 ImageWidth = image_width;
46 ImageHeight = image_height;
47 Position_X = position_x;
48 Position_Y = position_y;
49 Velocity_X = velocity_x;
50 Velocity_Y = velocity_y;
51 }
52 }
53
54
55
56 //========================================================================
57 //========================================================================
58 //========================================================================
59
60
61
65 public class Flyweight_Class
66 {
75
81
82
91 internal Flyweight_Class(BigResource resource, Flyweight_Context context)
92 {
93 _resource = resource;
94 _context = context;
95 }
96
97
102 {
103 get
104 {
105 return _context;
106 }
107 set
108 {
109 _context = value;
110 }
111 }
112
113
117 public int ImageWidth
118 {
119 get
120 {
121 if (_resource != null)
122 {
123 return _context.ImageWidth;
124 }
125 return 0;
126 }
127 }
128
129
133 public int ImageHeight
134 {
135 get
136 {
137 if (_resource != null)
138 {
139 return _context.ImageHeight;
140 }
141 return 0;
142 }
143 }
144
165 public void Render(List<char[]> display, int offset_x, int image_width, int image_height, int position_x, int position_y)
166 {
167 // Let the big resource handle the rendering at the given position.
168 _resource.Render(display, offset_x, image_width, image_height, position_x, position_y);
169 }
170 }
171
172
173
174 //========================================================================
175 //========================================================================
176 //========================================================================
177
178
179
193 {
194 List<string> _resource;
196
202 internal BigResource(List<string> resource, int resourceId)
203 {
204 _resource = resource;
205 _resourceId = resourceId;
206 }
207
218 internal void Render(List<char[]> display, int offset_x, int image_width, int image_height, int position_x, int position_y)
219 {
220 int display_width = display[0].Length;
221 int display_height = display.Count;
222 int starting_position_x = position_x;
223 int starting_position_y = position_y;
224
225 // Size of image to render (can be smaller than actual image if image
226 // lies partially off right or bottom of display).
227 int image_render_width = image_width;
228 int image_render_height = image_height;
229
230 // Position into image to start rendering from (non-zero if
231 // image is off the left or top edge of display).
232 int starting_row_in_image = 0;
233 int starting_col_in_image = offset_x;
234
235 // Clip the image to the display.
236 if (starting_position_x < 0)
237 {
238 starting_col_in_image = -starting_position_x;
239 image_render_width += starting_position_x;
240 starting_position_x = 0;
241 }
242 else if (starting_position_x + image_render_width > display_width)
243 {
244 image_render_width = display_width - starting_position_x;
245 }
246 if (starting_position_y < 0)
247 {
248 starting_row_in_image = - starting_position_y;
249 image_render_height += starting_position_y;
250 starting_position_y = 0;
251 }
252 else if (starting_position_y + image_render_height > display_height)
253 {
254 image_render_height = display_height - starting_position_y;
255 }
256
257 // If the image is even partially visible, render it
258 if (image_render_width > 0 && image_render_height > 0)
259 {
260 int current_display_row = starting_position_y;
261 int current_image_row = starting_row_in_image;
262 for (int row = 0; row < image_render_height; ++row)
263 {
264 for (int col = 0; col < image_render_width; ++col)
265 {
266 display[current_display_row][starting_position_x + col] = _resource[current_image_row][starting_col_in_image + col];
267 }
268 current_display_row += 1;
269 current_image_row += 1;
270 }
271 }
272 }
273
277 public int ResourceId
278 {
279 get
280 {
281 return _resourceId;
282 }
283 }
284
288 public int ImageWidth
289 {
290 get
291 {
292 if (_resource != null && _resource.Count > 0)
293 {
294 return _resource[0].Length;
295 }
296 return 0;
297 }
298 }
299
303 public int ImageHeight
304 {
305 get
306 {
307 if (_resource != null)
308 {
309 return _resource.Count;
310 }
311 return 0;
312 }
313 }
314
323 {
324 return new Flyweight_Class(this, context);
325 }
326 }
327
328
329
330 //========================================================================
331 //========================================================================
332 //========================================================================
333
334
335
350 public static class BigResourceManager
351 {
355 static List<BigResource> _resources = new List<BigResource>();
356
360 static int _nextResourceId = 1;
361
366 static int GetNextResourceId()
367 {
368 int nextId = _nextResourceId;
370 return nextId;
371 }
372
378 public static int AddResource(List<string> rawResource)
379 {
380 int newResourceId = GetNextResourceId();
381 BigResource addedResource = new BigResource(rawResource, newResourceId);
382 _resources.Add(addedResource);
383
384 return newResourceId;
385 }
386
393 internal static BigResource? FindResource(int resourceId)
394 {
395 BigResource? foundResource = null;
396 foreach (BigResource resource in _resources)
397 {
398 if (resource.ResourceId == resourceId)
399 {
400 foundResource = resource;
401 break;
402 }
403 }
404
405 return foundResource;
406 }
407
417 public static Flyweight_Class? CreateFlyweight(int bigResourceId, Flyweight_Context context)
418 {
419 Flyweight_Class? flyweightClass = null;
420 BigResource? bigResource = FindResource(bigResourceId);
421 if (bigResource != null)
422 {
423 flyweightClass = bigResource.CreateFlyweight(context);
424 }
425 return flyweightClass;
426 }
427 }
428}
Represents some big resource. In this case, a text "image" rendered as a list of strings....
int ImageHeight
Retrieve the "image" height of the resource.
BigResource(List< string > resource, int resourceId)
Constructor (accessibly only to the class factory).
Flyweight_Class CreateFlyweight(Flyweight_Context context)
Generate a Flyweight class that will represent this big resource in some context-dependent way.
int ImageWidth
Retrieve the "image" width of the resource.
void Render(List< char[]> display, int offset_x, int image_width, int image_height, int position_x, int position_y)
Render the big resource into the given display at the given position.
int ResourceId
Retrieve the resource ID for this resource.
Represents a manager for big resources. Also provides the class factory for the Flyweight_Class insta...
static int _nextResourceId
The next ID to assign a raw resource for management.
static List< BigResource > _resources
A list of all big resources managed by this class.
static int GetNextResourceId()
Retrieve the next resource ID that can be used.
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,...
static ? BigResource FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
Associates a context with a big resource.
BigResource _resource
The big resource being referenced by this flyweight class. In C#, a class is always a reference to an...
int ImageHeight
Retrieve the "image" height from underlying big resource.
Flyweight_Context _context
The context associated with this class. The calling entity uses this context to manipulate the flywei...
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_Class(BigResource resource, Flyweight_Context context)
Constructor (accessible only to the class factory).
Flyweight_Context Context
Retrieve or set the context for this class instance.
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...
Flyweight_Context(int offset_x, int image_width, int image_height, double position_x, double position_y, double velocity_x, double velocity_y)
Constructor.