association, aggregation and composition

折月煮酒 提交于 2019-12-31 17:12:51

问题


I'm dealing with this problem. I'm creating math problems, each one has response. For example.

  • If my question is about the "result of 5x + 15 = 2?", I'll be waiting just one answer (as integer).
  • If my question is about says "give me the area and permiter of this shape", I'll be waiting two answers (as doubles).
  • In another one, I'll be waiting as response a string
  • And anothers, I can have several answers or responses with various datatypes.

My big question is.

How would be the relation between the classes question and response. Also I was dealing if this should be an association, aggregation or composition.

Thanks.

EDIT: Great, it's a composition. Last thing, according to above sentences, how can I represent the design? These are some ideas what I have, but I guess I'm wrong.

public class Question
{
    public Response _response;
    //public List<Response>
    //public Dictionary<string, Response>

    public Question()
    {
        this._response = new Response();
    }
}

public class Response
{
}

回答1:


Association is a relationship where all object have their own life cycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

Aggregation is a specialized form of Association where all objects have their own lifecycle but there is an ownership: a child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department the teacher object will not be destroyed. We can think about it as a “has-a” relationship.

Composition is again a specialized form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child objects does not have their life cycle and if the parent object is deleted all child objects will also be deleted. Let’s take again an example of relationship between House and rooms. A house can contain multiple rooms and there is no independent life for a room and a room can not belong to two different houses. If we delete the house its rooms will be automatically deleted. Let’s take another example relationship between Questions and options. Single questions can have multiple options and an option can not belong to multiple questions. If we delete a question its options will also be deleted.




回答2:


There is an association between these two.

Question composes answers.

The reason for this is - aggregation and composition are both associations. Composition means that the child object's lifetime depends on its parent - child cannot exist without parent.

This is exactly your case here. Thus - composition.



来源:https://stackoverflow.com/questions/11265711/association-aggregation-and-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!