views:

64

answers:

1

I'm trying to implement the Strategy pattern using Core Data and Objective C.

To simplify, I have, say, a multilingual Text entity, which has several attributes, such as a Date for last saved, and a NSSString for author etc etc. I want to add a reference to a Language entity, which could be one of several subclasses, such as French, Italian and so on, holding an NSString of text in the language of the class name. Each of the Language subclasses can implement their own translateTo and translateFrom methods and the Text instance doesn't need to worry about it. Simple Strategy Pattern.

Created by yuml.me

I'm really struggling to set up this kind of relationship with Core Data, where a certain Text entity can be set up with its own Language implementation depending on the details of the instance.

I suppose in a nutshell, what I'm trying to do is create a "has-a" relationship, where the entity inside the Text entity can be one of several subtypes of Language.

Core Data is quite new to me, so if I'm completely missing the point here, or have missed something glaringly obvious, please let me know.

+3  A: 

You can have abstract entities. I'd create a Language abstract entity, a to-on relatioship between Text and Language, and concrete subentities of Language called Italian, French etc. Example with the Xcode editor:The Xcode editor showing a diagram much like the one in the original question

You would then set the .language relationship of your Text with a new Italian or French NSManagedObject as needed.

millenomi
Be aware of a potential performance issue with this concept. Each entity that is a subentity will be placed into a single table. Therefore in this example, Italian and French would actually live in the same table and potentially creating a lot of void space.
Marcus S. Zarra
Thanks - it turns out what I was missing was the designation of the parent on the entity. Doesn't seem so obvious how to do that in the new version of Xcode - back on Xcode 3 and it's all good. Thanks again.
Noel M