开发者

Some specific questions about object oriented and MVC design

开发者 https://www.devze.com 2022-12-24 05:13 出处:网络
I have two objects, Users and Mail. Users create Mail objects and send them to other users. If I wanted to get all mail for a User, I could create a method like GetMail() that would return an array

I have two objects, Users and Mail. Users create Mail objects and send them to other users.

  1. If I wanted to get all mail for a User, I could create a method like GetMail() that would return an array of Mail objects owned by that User.
  2. But if I wanted to get all mail across the system, what "type" of object would be responsible for that开发者_StackOverflow?
  3. To solve this problem, I usually create a Manager, which is an object responsible for dealing with a collection of a particular type of object. MailManager deals with collections of Mail objects. GetMailForUser() is one method, GetAllMail() is another method. The User objects invokes the MailManager and executes GetMailForUser(me). Is this stupid?
  4. When a user executes the controller CreateMail, a new instance of the Mail object is created. The Mail object, seeing it is creating a new Mail of type 'sent', decides to go ahead and create a second Mail object for the recipient, of type 'received'. Creating one Mail object triggers the creation of a second Mail object. Is this stupid? Should the controller have created both Mail objects, or just the first 'sent' one?
  5. When two Users are friends, the association is stored in a table of Relationships. I use a simple object for Relationships. A RelationshipManager has a method called GetFriendsForUser(). The User object has a method GetFriends(), which invokes the RelationshipManager. Is this stupid?


  1. IMHO according to OOP, getAllMail is message which might be reasonable to put in Mail class as a static method. In languages where collections aren't first-class citizens, classes tend to be used as set of objects with same behaviour, state or identity. So naturally, to obtain the set of objects of some class, you have to ask that set for it.
  2. Definitely it is not stupid, it always depends on context. You should ask yourself, if you need a separate singleton class for managing objects. Will you use that object separately from the Mail class? Do you want to inherit from it or use some other oo mechanism?
  3. Hmm, this is a design, which I cannot really grasp, can you state what benefits it gets you? I don't know exactly what is your context, but I would probably just have Mail objects with two Users associations, one for the sender and the other one for recipient. This will save half the objects and ease navigation through the object graph from one user through mail to other user.
  4. Again, depends on your context and advantages you get from the separation.
0

精彩评论

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