关联,聚合和组合 [英] association, aggregation and composition

查看:152
本文介绍了关联,聚合和组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理这个问题。我创建的数学题,每一个都有回应。例如。




  • 如果我的问题是关于5倍+ 15 = 2的结果呢?后,我会等待只有一个答案(作为整数)。

  • 如果我的问题是关于说:给我这种形状的面积和permiter,我会等待两个答案(双打)。

  • 在一个又一个,我会等待响应作为一个字符串

  • 并anothers,我可以有几个答案或响应各种数据类型。



我的大问题是



如何将是类问题和响应之间的关系。 。我也正在处理这应该是一个关联,聚合或组合



感谢



编辑:大,这是一个成分。最后一件事,按照上面的句子,我怎么能代表设计?这些都是一些想法我有什么,但我想我错了。

 公共类问题
{
公开回应_response;
//公开名单<应变及GT;
//公众解释<字符串,响应>

公众质疑()
{
this._response =新的响应();
}
}

公共类响应
{
}


解决方案

协会是有关系的,所有物体都有自己的生命周期,也没有老板。让我们师生的例子。多个学生可以用一个老师关联,一个学生可以与多个教师交往,但有对象和两者各有自己的生命周期之间没有所有权。 。既可以创建单独删除



汇聚是协会的一种特殊形式,所有物体都有自己的生命周期,但有一个所有权:子对象不能属于另一个父对象。让我们部和老师的例子。一名教师不能属于多个部门,但如果我们删除该部门老师对象不会被破坏。我们可以把它作为一个有的关系。



组成又是聚合的一种特殊形式,我们可以把这个作为死亡的关系。这是一个强类型聚集。子对象没有自己的生命周期,如果父对象被删除所有子对象也将被删除。让我们再次拿房子和房间之间的关系的一个例子。房子可以包含多个房间,没有独立生活的房间和一间不能属于两个不同的房子。如果我们删除其屋客房将被自动删除。让我们问题和选项之间另一个例子关系。单个问题可以有多种选择和选项不能属于多个问题。如果我们删除问题的选项也将被删除。


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
{
}

解决方案

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.

这篇关于关联,聚合和组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆