1

I know according to open closed principle, something like class,interface,code.... should be unchanged when add new functions or contents, but is file also in this case?

I was designing a game, the game have some monster information, and the game (in client side) may download or replace some file if there are new monsters to add or update.

I was worrying what should be the format of monster information: 1 file with all monster information or each monster has individual file?

1.Store all monster information into a single file (e.g.:monsterlist.json),each time add new monster it will replace the file e.g.:

{  
    "monsterList":[
        {
            "id":1,
            "hp":100,
            "attack":230,
        },
        {
            "id":2
            "hp":500
            "attack":980
        }
    ]
}

2.Each monster has a individual file,each time add a new monster will also add a new monster json:

monster1.json

{
    "id":1,
    "hp":100,
    "attack":230,
}

monster2.json

{
    "id":2,
    "hp":500,
    "attack":980,
}

At start I think case 1 is most convenient, because when I add or update any monsters I only need to replace monsterlist.json, but for case 2, I need to know which json is new (may need extra row to store the update status in server database).

But suddenly I remember open closed principle, adding something new should not modify the current content, it seems not reasonable to replace a file when a new monster is added.

The above is just the example of why I think if "file" includes in open closed principle, not asking which format is better. The main point I want to ask is not which file format is better, but the definition of open closed principle, because I cannot find any definition that mentions "file", once it is confirmed, I can deduce which format of file should use.

ggrr
  • 7,737
  • 5
  • 31
  • 53
  • Have you thought about using a database instead of files for this? Have a look at sqlite – codebox Aug 17 '15 at 09:54
  • The principle you cite pretty much only pertains to *code*, not data. Files are a data storage mechanism. – deceze Aug 17 '15 at 09:55
  • 1
    @deceze: On the other hand, applying the principle to data, in this case files, makes the creation of content/mods much easier. One extreme example of applying this principle to data is web servers - adding new content to your website does not require you to recompile Apache. Games tend to follow this approach. – slebetman Aug 17 '15 at 12:53
  • @amuse Just stay pragmatic and keep it simple and efficient. You could go a long way with a single file and a proper source control tool, but if a single file becomes a problem you may transition to separate files or even a different kind of storage mechanism. – plalx Aug 17 '15 at 15:54

0 Answers0