Trying to put my head around how to model the notion of default values in an object hierarchy. These default values should apply to all objects in the hierarchy unless an object overrides a setting. If a setting is overridden, then it and all of its children would get the overridden value, but other values would be pulled from up the hierarchy.
Maybe an example would help. Consider a system that models the scheduling of sports games. At the top, there is a Sport object. This object contains a Set of Leagues, and a League contains a set of Seasons. A Season contains a set of Schedules, and a Schedule contains a set of Games:
class Sport {
private Set<League> leagues;
}
class League {
private Set<Season> seasons;
}
class Season {
private Set<Schedule> schedules;
}
class Schedule {
private Set<Game> games;
}
class Game {
}
Maybe there is a Defaults object that is somewhat like this:
class Defaults {
private Set<Venue> venues;
private Set<Weekday> weekdays;
private int gameDuration;
private Set<RefereePositions> requiredRefereePositions;
}
So, a Sport object for "Soccer" would own a Defaults object that specifies:
venues: ["Field 1","Field 2", "Field 3", "Field 4", "Field 5"}
weekdays: ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]
gameDuration: 90
requiredRefereePositions: ["Referee","Linesman 1","Linesman 2"]
Then, a League could override those settings, perhaps:
venues: ["Field 4","Field 5"]
weekdays: ["Sat","Sun"]
A Season could override the settings, as could a schedule. Perhaps a certain schedule of games is for low level games and only need one referee. So that Schedule might override Defaults with:
requiredRefereePositions: ["Referee"]
So, this means that when the scheduling system runs and attempts to schedule a Game within this Schedule, it would know that the game should be scheduled at either "Field 4" or "Field 5", should be on Sat or Sun, should be 90 minutes long, and have one referee:
venues: ["Field 4","Field 5"]
weekdays: ["Sat","Sun"]
gameDuration: 90
requiredRefereePositions: ["Referee"]
What would be a good way to model this? Is there an existing design pattern for it? I've been trying to Google for solutions, but am having a hard time picking the right keywords. If anyone can point me at online resource or examples of doing this, I would much appreciate it! I'm implementing this in Java, but will look at examples in anything.
One technology that I can think of that is sort of similar is how CSS in a browser works. Values are cascaded down the hierarchy, with each node being able to override values from higher up. Would this be the right approach? How would I model that in Java? How would I persist it and query it via Hibernate?
My last concern is how to handle default settings that only pertain to a certain set of objects in the hierarchy. For instance, in CSS the value "width" probably makes no sense for a 'script' element. In CSS, that setting is probably just ignored, but if I did that, my Defaults object would be this big class with every possible setting that any object in the system might need. What does "requiredRefereePositions" mean to a Venue object? Not sure if that's the best way to go.