Design Pattern Examples
Overview of object-oriented design patterns
Flyweight_Classes.h
Go to the documentation of this file.
1
8
9#pragma once
10#ifndef __FLYWEIGHT_CLASSES_H__
11#define __FLYWEIGHT_CLASSES_H__
12
13#include <memory>
14#include <string>
15#include <vector>
16
18{
19
29 {
33 double Position_X;
34 double Position_Y;
35 double Velocity_X;
36 double Velocity_Y;
37
43 , ImageWidth(0)
44 , ImageHeight(0)
45 , Position_X(0.0)
46 , Position_Y(0.0)
47 , Velocity_X(0.0)
48 , Velocity_Y(0.0)
49 {
50 }
51
63 Flyweight_Context(int offset_x, int image_width, int image_height, double position_x, double position_y, double velocity_x, double velocity_y)
64 {
65 OffsetXToImage = offset_x;
66 ImageWidth = image_width;
67 ImageHeight = image_height;
68 Position_X = position_x;
69 Position_Y = position_y;
70 Velocity_X = velocity_x;
71 Velocity_Y = velocity_y;
72 }
73 };
74
75
76
77 //========================================================================
78 //========================================================================
79 //========================================================================
80
81 class BigResource; // forward reference
82
87 {
88 public:
89 using unique_ptr_t = std::unique_ptr<Flyweight_Class>;
90
91 private:
100
106
107 public:
112 : _resource(nullptr)
113 {
114 }
115
125
130
135
136
140 void SetContext(Flyweight_Context context);
141
142
146 int ImageWidth();
147
148
152 int ImageHeight();
153
174 void Render(std::vector<std::vector<char>>& display, int offset_x, int image_width, int image_height, int position_x, int position_y);
175 };
176
177 using FlyweightClassList = std::vector<Flyweight_Class::unique_ptr_t>;
178
179
180 //========================================================================
181 //========================================================================
182 //========================================================================
183
184
185
199 {
200 public:
201 using unique_ptr_t = std::unique_ptr<BigResource>;
202
203 private:
204 std::vector<std::string> _resource;
206
207 public:
213 BigResource(std::vector<std::string> resource, int resourceId)
214 {
215 _resource = resource;
216 _resourceId = resourceId;
217 }
218
220
221
232 void Render(std::vector<std::vector<char>>& display, int offset_x, int image_width, int image_height, int position_x, int position_y);
233
237 int ResourceId();
238
242 int ImageWidth();
243
247 int ImageHeight();
248
257 };
258
259
260
261 //========================================================================
262 //========================================================================
263 //========================================================================
264
265
266
282 {
283 private:
284 using BigResourceList = std::vector<std::unique_ptr<BigResource>>;
285
290
294 static int _nextResourceId;
295
300 static int GetNextResourceId();
301
308 static BigResource* FindResource(int resourceId);
309
310 public:
316 static int AddResource(std::vector<std::string> rawResource);
317
327 static Flyweight_Class::unique_ptr_t CreateFlyweight(int bigResourceId, Flyweight_Context context);
328 };
329
330} // end namespace
331
332#endif // __FLYWEIGHT_CLASSES_H__
333
Represents some big resource. In this case, a text "image" rendered as a list of strings....
BigResource(std::vector< std::string > resource, int resourceId)
Constructor (accessibly only to the class factory).
int ResourceId()
Retrieve the resource ID for this resource.
void Render(std::vector< std::vector< 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.
Flyweight_Class::unique_ptr_t CreateFlyweight(Flyweight_Context context)
Generate a Flyweight class that will represent this big resource in some context-dependent way.
std::unique_ptr< BigResource > unique_ptr_t
std::vector< std::string > _resource
int ImageHeight()
Retrieve the "image" height of the resource.
int ImageWidth()
Retrieve the "image" width of the 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.
std::vector< std::unique_ptr< BigResource > > BigResourceList
static int GetNextResourceId()
Retrieve the next resource ID that can be used.
static BigResourceList _resources
A list of all big resources managed by this class.
static BigResource * FindResource(int resourceId)
Retrieve the BigResource corresponding to the specified ID.
static int AddResource(std::vector< std::string > rawResource)
Add a new big resource and return the ID of the resource.
static Flyweight_Class::unique_ptr_t 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.
void Render(std::vector< std::vector< 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
The context associated with this class. The calling entity uses this context to manipulate the flywei...
BigResource * _resource
The big resource being referenced by this flyweight class. In C#, a class is always a reference to an...
Flyweight_Context Context()
Retrieve the context for this class instance.
void SetContext(Flyweight_Context context)
Set the context for this class instance.
std::unique_ptr< Flyweight_Class > unique_ptr_t
int ImageHeight()
Retrieve the "image" height from underlying big resource.
int ImageWidth()
Retrieve the "image" width from underlying big resource.
The namespace containing all Design Pattern Examples implemented in C++.
std::vector< Flyweight_Class::unique_ptr_t > FlyweightClassList
Represents a big image. Call the BigResource_Clear() function to release the memory used by each inst...
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.