Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flyweight [Design Pattern]
#1
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 {
    constructor(x,y,z,angle,world,hp,str,range,_name)
    {
        x = x;
        y = y;
        z = z;
        angle = angle;
        world = world;
        hp = hp;
        str = str;
        hpleft = hp;
        range = range;
               name = _name;
    }
    
    x = null;
    y = null;
    z = null;
    angle = null;
    world = null;
    hp = null;
    str = null;
    hpleft = null;
    range = null;
       name = null;
};

local wolfs = [
MobIn(11,4120,10,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(12,10,14210,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(1123,1124,14124,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(14,10,12130,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(1125,141,10,5,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(16,10,11230,10,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(1712,4120,112540,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(1812,10,1140,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(10,1210,1041,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
MobIn(1210,10,11240,0,"NEWNEWWORLD//NEWWORLD.ZEN",40,10,1000,"Wolf"),
]

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.

// creating a hash table, which will contain shared statistics
local WolfStat = {
   maxHp = 40,
   str = 10,
   dex = 10,
   range = 100,
   name = "Wolf",
}

class MobIn {
   // shared stats between all objects
   static stats = WolfStat
   
   // initial fields which will be later copied for each instance
   x = null
   y = null
   z = null
   angle = null
   world = null
   
   hp = null

   constructor(_x,_y,_z,_angle,_world, _hp)
   {    
       // creating copy of these fields
       x = _x;
       y = _y;
       z = _z;
       angle = _angle;
       world = _world;
       
       hp = 40
   }
};

local wolfs = [
MobIn(11,4120,10,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(12,10,14210,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(1123,1124,14124,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(14,10,12130,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(1125,141,10,5,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(16,10,11230,10,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(1712,4120,112540,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(1812,10,1140,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(10,1210,1041,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
MobIn(1210,10,11240,0,"NEWNEWWORLD//NEWWORLD.ZEN", 40),
]


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.
Reply


Messages In This Thread
Flyweight [Design Pattern] - by Quarchodron - 11.02.2019, 10:53
RE: Flyweight [Design Pattern] - by Patrix - 11.02.2019, 14:26
RE: Flyweight [Design Pattern] - by Patrix - 16.09.2019, 20:32

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decorator [Design Pattern] Patrix 1 2,717 10.09.2019, 07:56
Last Post: Quarchodron

Forum Jump:


Users browsing this thread: 1 Guest(s)