Adapter public functions in c++ - good? bad? even a real pattern? -
i use pattern in code , i'm wondering if it's good, bad, or has name. i've done research see if it's commonly used pattern solve issues, can't seem find anything. helps me prevent knowledge of memory being available many colleagues or peer objects. guess example describe best.
suppose have game player has inventory. inventory has items, , each item has databaseid, 2 copies of same item can point same database entry. when needs take item player's inventory, don't have explicit knowledge of item itself, , instead know item db id need remove. additionally, items stored handleid (unique), used client user interfaces (the unique handle passed interface uiitem has sort-of callback id)
psuedo-code:
class inventory map<int, item> m_items; public: bool removeitem(int dbitemid, int count) { item* pitem = getitembydbid(dbitemid); if (pitem) { return removeitem( pitem, count); } return false } bool removeitem(int handle, count) { item* pitem = m_items.find(handle); //find in map returns const pointer. if (pitem) { return removeitem( pitem, count); } return false; } private: bool removeitem(item* item, int count); //handles logic of removing, removes, , returns true if remove succeeds (ie, quest items can't removed, etc) item* getitembyhandle(int handle); //returns item in map handle, null if not found item* getitembydbid(int dbid); //returns first item dbid, null if not found. now, if ui wants remove item (ie, player dragged trash bin) there public method decouples knowledge. if npc wants remove item (lets npc steals when touches player, , looking particular itemid) has public method decouples knowledge.
my question firstly, pattern have name, or variable of pattern i'm not seeing? secondly, practice or bad practice? me, it's good, might missing something.
looks me textbook example of flyweight pattern.
a flyweight object minimizes memory use sharing data possible other similar objects; way use objects in large numbers when simple repeated representation use unacceptable amount of memory. parts of object state can shared, , common practice hold them in external data structures , pass them flyweight objects temporarily when used.
Comments
Post a Comment