开发者

How to model cascading settings in an object hierarchy

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开发者_Go百科;
    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.


If you look at the Properties class, I think it will provide you with either a solution, or a strategy for a solution.

I would encourage you to not attempt to put a getter for default values into your object model - that violates the principle of separation of concerns - instead, pass in the properties to the constructor. More than likely, the built in Properties class will do what you are looking for, and provides the level of decoupling that you are asking about.

If you aren't happy doing string based lookups, you could implement something similar using enums, or even specific getXXX calls in your own custom class. The key is that the class will receive a delegate in it's constructor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜