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 Strate开发者_JS百科gy Pattern.
Created by yuml.me http://yuml.me/1caedd69
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.
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 http://files.droplr.com.s3.amazonaws.com/files/762938/1u3s2W.Screen%20shot%202010-08-05%20at%2010%3A50%3A57.png
You would then set the .language relationship of your Text with a new Italian or French NSManagedObject
as needed.
精彩评论