Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

association, aggregation and composition

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
{
}
like image 928
Darf Zon Avatar asked Jun 29 '12 16:06

Darf Zon


1 Answers

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.

like image 126
Irfan Lateef Avatar answered Sep 29 '22 14:09

Irfan Lateef