![]() |
Flyweight [Design Pattern] - Printable Version +- Gothic Online Forums (https://archive.gothic-online.com) +-- Forum: Scripting (English) (https://archive.gothic-online.com/forum-11.html) +--- Forum: Tutorials (https://archive.gothic-online.com/forum-13.html) +--- Thread: Flyweight [Design Pattern] (/thread-2650.html) |
Flyweight [Design Pattern] - Quarchodron - 11.02.2019 Flyweight
Hi, today i'm gonna show you guys how to use flyweight pattern in practice. But first what is flyweight. Theory In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory usage by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory. Often some parts of the object state can be shared, and it is common practice to hold them in external data structures and pass them to the objects temporarily when they are used. A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor. It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally. Another example is string interning. In other contexts the idea of sharing identical data structures is called hash consing. from : wikipedia.org Practice In practice specially when we talk about using a this pattern in G2O. We for sure wanna use it on systems like creating mobs, vobs etc. Everything what need to have a lot of objects. Becouse the pattern is to save memory. We must ensure that there are no repeating constant variables in the object. Well just i will show that on practice. In this part i will try to create 10 wolfs. Code: class MobIn { Well as you see there is like ten wolf with same statistics same range. Only position is different. So we need to have more space to insert this statistics. So its kinda wasting of resources. One of better way to do it is just to add class (Singleton) with will have all this statistics. Look. Code: /// We dont have singletone class so we use table. Well its like okey thats nice but we could use other way to dont write it same times. Yes but that not the point. You have a lot of ways to do the same thing. Just to reach a goal with is save a memory. RE: Flyweight [Design Pattern] - Patrix - 11.02.2019 Your usage example isn't correct (all fields are copied anyway). Good post, correct the example, and i hope, that you will add some more similar posts ![]() RE: Flyweight [Design Pattern] - Quarchodron - 11.02.2019 Correct version update - thx to Patrix RE: Flyweight [Design Pattern] - Patrix - 16.09.2019 It's worth to mention that this design pattern can be implemented to the existing code. The current example (made by me) isn't showing this way, so i'm going to demonstrate one neat trick with use of metatables that allows us to easilly edit the existing class to use the flyweight pattern. Suppose that we have a class called Npc, let's just assume that it looks like more or less like this: Squirrel Script
The problem is, that if we change this class by adding the static keyword before the attributes like this: Squirrel Script
The error will occure when we try to get the static field via the object like this: Squirrel Script
What we can do, is to add the _get metamethod which will be invoked each time, when we try to access the object field which doesn't exists. Squirrel Script
Now, when we try to access the field via the object, everything will be working as it should. There is only one problem, having a static fields as types: int, string, null, bool, float will cause a problem with directly trying to modify them. First of all, we can't modify the static field via the object, even if we provide the _set metamethod, it will still raise an error. There is a way to add the desired functionallity by using the class.newmember or class.rawnewmember. Squirrel Script
IMO however it's not required to supply the _set metamethod. Anyways, that's all i wanted to show, ofc _get metamethod can be implemented in different way allowing us to access the data from for example a table. |