'Master Object' needs to exist without database calls
We're building a game, so that may help to put the objects into context better.
I have a situation where I have a structure kind of like this..
- Template
- Data(ICollection<Statistics>)
- Character: Template
- Data (ICollection<Statistics>)
To elaborate... Let us assume that every character has a property "Health". Now, the default for "Health" is 15.
Now let us assume that every character starts with that. As a character over its lifetime, it might add to "Health" with new values. But they still need to retain the default reference.
Now, 'Character' inherits defaults from 'Templ开发者_开发技巧ate', but each character will have its own set of data that appends the defaults. Raw inheritance does not work because the Item appends, it does not overwrite. The original defaults still need to exist.
I can solve it like this..
- Character
- Template
- Data (ICollection<Statistic>)
But this is redundant to me. It requires a lot of extra database calls. Basically every call to a item has to do the same code twice because it has to construct a Template object too.
Is there a more logical way to go about this?
If I'm understanding you correctly, it sounds like you want to have static values in the template, and then dynamically update the inheritors of those values? If all characters need to have the same default data, can you do something like this?
abstract class Template {
ICollection<Statistics> DefaultData;
}
class CharacterTemplate : Template {
private CharacterTemplate() {
//appropriate data initialization
}
private static readonly CharacterTemplate _instance = new CharacterTemplate();
public static Template Instance { get { return _instance; } }
}
class Character {
Template template = CharacterTemplate.Instance; /* CharacterTemplate */
ICollection<Statistics> Data ;
}
Have the Template implementors implement the Singleton pattern, and you have to initialize it from the database at most once.
For the background on the singleton pattern, check wikipedia: http://en.wikipedia.org/wiki/Singleton_pattern#C.23
精彩评论