3

I have to say I always try to keep code simple and beautiful, mainly using design patterns when possible. Also, I am impressed I did not find anything related to this on the internet (except simple and very vague examples, mostly in javascript using json).

The scenario is: I have to parse/build concrete objects from a file, whose content may be XML, JSON and/or other formats. Heres an example:

Concrete object:

// Contains the common states for the entities
struct EntityModel
{
int hp;
int level;
int armor;
int speed;

// Other attributes...
};

class Entity
{
// Stuff (protected/public/private attributes and functions/methods)

private:
EntityModel* m_model; // Pointer to the model used (flyweight)

// Other attributes...
}

File (XML, in this case):

<entity name="Skeleton" class="Undead">
   <attributes>
      <hp value="150" />
      <level value="10" />
      <armor value="75" />
      <speed value="15" />
      <width value="32" />
      <height value="32" />
      <experience value="372" />
      <texture value="skeleton.png" />
      <intelligence value="skeleton.script" />
   </attributes>
   <restistances>
      <resist type="Shock" value="30" />
      <resist type="Fire" value="10" />
   </resistances>
   <attacks>
      <spell name="Blizzard" mp="50" damage="130" distance="0" />
      <spell name="Fireball" mp="30" damage="100" distance="0" />
   </attacks>
   <loot>
      <drop item="Gold Coin" min="30" max="50" probability="1" />
      <drop item="Ruby" min="0" max="2" probability="0.7" />
      <drop item="Enchanted Sword" probability="0.25" />
   </loot>
</entity>

This is the example of the relationship between an entity model and its file. There will also be other objects that have to be able to be parsed/built/created from their files.

Some may say that a design pattern is not really necessary in this case, as I have seen in a few implementations, although I do really believe there is one. The whole entity creation system involves the abstract factory, pool and flyweight patterns (a createEntity call is requested to the factory, which will see if a flyweight model has already been created and cached in the pool or create and cache the new model).

So, the question is: Are there any proper way to do that? Which one?

I'll be basing on the answer for this very case and adapt to the other object creations, as I have stated. In other words, I need a generic answer.

If this post is missing some information, or is in a wrong section, please forgive me as this is my first post here.

Thanks in advance.

Yves Calaci
  • 1,019
  • 1
  • 11
  • 37
  • 1
    Some days, design patterns are not needed. There is not a design pattern for everything, nor does there need to be. – Thomas Matthews Oct 15 '15 at 01:17
  • The proper way to read an XML file is to use an XML library. Don't waste your time reinventing the wheel. – Thomas Matthews Oct 15 '15 at 01:17
  • Hey, Thomas. Thanks for your time. I'm using the RapidXML library to parse XML files. I am not creating a new XML parser. What I am asking for is a way to use that parser to build objects in a fashion way using design patterns which I believe there is one which fits this case. – Yves Calaci Oct 15 '15 at 01:28
  • You probably want a derivative of the *Factory Design Pattern*. – Thomas Matthews Oct 15 '15 at 15:23

2 Answers2

0

Try the Boost Serialization Library. It has xml, binary, and text save formats. It's not too complicated and has good documentation.

fuzzything44
  • 701
  • 4
  • 15
  • From what I could see, that boost serialization library only offers object serialization to documents. I don't want that. Still, I can't use boost (for various purposes like many other people). – Yves Calaci Oct 15 '15 at 15:02
0

I recommend a derivative of the Factory Design Pattern.

The pattern allows you to construct objects based on a criteria, such as a name or number. The traditional pattern creates objects based on a common base class.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Hey, Thomas. That's exactly what's been implemented so far. To create an Entity (game living object), a createEntity call is requested to the EntityFactory. That factory will look in its internal cache if a model has already been created for the specified entity ID. If it did not, the model has to be created (parsed from a file), cached and returned to create the entity (the entity's model pointer points to the fetched model - see example above). The problem lies in the parsing code. Are there any design pattern for this (generic parsing from file to object, using an existing parse library)? – Yves Calaci Oct 15 '15 at 17:17
  • I don't understand. In one comment you say you are using the RapidXML library to parse the file, yet you want a design pattern for parsing the file. I'm assuming you get the object name from the XML and then use a factory to create the object. What part of the design are you looking for a design pattern? – Thomas Matthews Oct 15 '15 at 17:26
  • Sorry, it's been a while. The parsing (bytes to XML file structure) is done by the RapidXML lib. I'm looking for a design pattern to use that XML file object to make concrete objects, for example: suppose we have a class XMLEntityFile (see above for attributes) that contains the parsed xml nodes and an Entity class with the very same attributes, plus new ones (like few pointers). What pattern could I use to create Entities using that XMLEntityFile? Should I use builder? Friendship or inheritance? Hope its clear now. Cheers! – Yves Calaci Nov 04 '15 at 20:10