开发者

Simple OO question: Which of these classes should own this operation?

I was working on a UML class model and (basic as it is) I feel I need to see if my assumption is right. I have two types: Administrator and Location. (A relationship exists between the two where an Administrator can be at one or more locations).

An Administrator needs to assign themselves to a Location. Should this assignment operation be in the Location type or t开发者_StackOverflow中文版he Administrator type? What is the reasoning for one over the other?


As I see it, the Administrator would be the class having a state, and thus knowing where he is. The location is a data-entry, and should be stateless.

Hence, it's the administrator where you would add or remove locations.

Ie. typical C#:

Administrator.Locations.Add
Administrator.Locations.Remove

Also, I don't see how it could in the Location type, without you using two-way relations, which doesn't seem appropriate here.


Both are possible and with argumentation, both could also be just fine.

If the administrator is logged in at the main server, he could invoke AdminUtils.Locations(That).AddAdministrator(Me). On the other hand, if the administrator is at a specific location he could, prior to logging in at that location, invoke AdminUtils.Administrators(Me).AddLocation(This).

These kind of questions are for specific situations generally answered by considering responsibilities. Which entity is responsible for which entities?

Also note that these Add methods could perfectly be elements of both entities, but one of them is mapped to the actual storage which should be just one.


It depends on what is your software about. If it's a database about people, then Administrator will contain list of assigned places(addLocation / removeLocation), if it's about places, then Location should reference its Administrator(setAdministrator). If you track the history of assignments then obviously Assignment will be a separate entity. Such things are very well described in DDD Book by Eric Evans

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜