Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Context

In a Cocoa application I am developing, I need to load a data model that is defined in plist files. So, I have something like:

root (Array)
---- item (Dictionary)
-------- name : "objects" (string)
-------- prefab : "object" (string)
-------- children : (Array)
...
---- item (Dictionary)
-------- name : "actions" (string)
-------- prefab : "action" (string)
-------- children : (Array)

So, the previous data model is loaded in the code to become an object representation using the language's primitives (arrays, dictionaries, strings, ... etc). No custom classes here. When I load that model, I know that an item in the table view can have children if the "prefab" key is set. When "add child" is pressed, I go to the prefabs (some other plist) and fetch that prefab's structure. e.g:

root (Array)
---- item (Dictionary)
-------- name : "object" (string)
-------- parameters : (dictionary)
------------ key "object_id" : value ""
------------ key "x_position" : value 0
...

As you can see, from the prefab in the first model, I can go to the second plist and fetch a structure that is defined in a plist. All loaded using a plist library, without any subclass creation whatsoever. There is a single prefab factory class that contains the prefab structures, and instantiates and initializes the object from the prefab with the configuration data.

Problem

I hope you see the grave danger in this design. The model contains both user generated data as well as configuration data!! EEEEK!!! ... Sheesh, that's so bad...

The problem haunted me when I started exporting the model after the user of the app interacted with the model though the UI. The exported model would contain the configuration data (such as the prefab key), which makes the exported data rigid and un-upgradable. If I were to change the prefab key in later version of the app, to something else, and you load an old model, that model will load the old configurations with it, in our case, referencing the old prefab.

Question

I need help find a proper design pattern that clicks with this data driven design that I have, by providing the configuration data from a separate file, and somehow, linking it with the data model.

share|improve this question
    
I don't know cocoa, so my comment might not apply to your problem. You could have a class that implements two interfaces. One representing the generated data, the other representing the configuration data. They would represent the two facets of the same object. –  Olivier Jacot-Descombes Dec 13 '13 at 0:08
    
@OlivierJacot-Descombes Interesting, sounds like a facade kind of thing. The idea of creating a class definitely sounds like the way to go, as for the two interfaces, it won't prove useful in cocoa, since I am using a very loosely coupled Key-Value Coding thing, so the type is completely abstracted. Indeed, I am declaring them as id when performing operations on those objects. –  Mazyod Dec 13 '13 at 0:55

1 Answer 1

up vote 0 down vote accepted

Funnily, while writing the question part of the post, I wrote this:

I can't create subclass for each data entity (object, action, ... etc), but I can create a generic subclass that fits all the objects.

That's it!

Even though the prefab factory can configure the instantiated object on-the-fly, it can only do that when the object is created for the very first time. When it is loaded from the model, it is more or less de-serialized, which meant I had to traverse the model tree... Which was so ugly, I couldn't write such code. Writing a custom subclass to rule them all, I can have it configure itself even when loaded from a serialized data model.

(Final remarks: I didn't want all that typing in the question to go to waste, besides stackexchange promotes Q&A style spreading knowledge kinda thing, so, yay)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.