C#中组合和聚合的实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/759216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implementation of composition and aggregation in C#?
提问by 123Developer
How do I/(what is the best way to) implement composition and aggregation in C#?
我/(最好的方法是什么)在 C# 中实现组合和聚合?
Thanks 123Developer
感谢 123Developer
采纳答案by Alconja
That's a pretty abstract question, given that both composition & aggregation are pretty similar & really only different conceptually and not necessarily at the code level. (i.e. you might consider a Car having an Engine to be composition, and a Dog having fleas to be aggregation, but there's nothing stopping you implementing them in the same way if you were modelling them in code).
这是一个非常抽象的问题,因为组合和聚合非常相似,实际上只是在概念上有所不同,而不必在代码级别上有所不同。(即,您可能将具有引擎的汽车视为组合,将具有跳蚤的狗视为聚合,但如果您在代码中对它们进行建模,则没有什么可以阻止您以相同的方式实现它们)。
However, if you want to break down the differences & try & forcibly add software design decisions to highlight those differences I guess you could do something like this... taking an example from Wikipedia:
但是,如果您想打破差异并尝试强行添加软件设计决策以突出这些差异,我想您可以这样做……以维基百科为例:
Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.
聚合与普通组合的不同之处在于它并不意味着所有权。在组合中,当拥有对象被销毁时,所包含的对象也会被销毁。在聚合中,这不一定是正确的。例如,一所大学拥有多个系(例如化学),每个系都有多个教授。如果大学关闭,院系将不复存在,但这些院系的教授将继续存在。因此,大学可以被视为系的组合,而系则是教授的集合。此外,一个教授可以在多个系工作,但一个系不能隶属于多个大学。
You might build this code to represent it (with as many contrived indications of composition/aggregation):
您可以构建此代码来表示它(使用尽可能多的组合/聚合指示):
public class University : IDisposable
{
private IList<Department> departments = new List<Department>();
public void AddDepartment(string name)
{
//Since the university is in charge of the lifecycle of the
//departments, it creates them (composition)
departments.Add(new Department(this, name));
}
public void Dispose()
{
//destroy the university...
//destroy the departments too... (composition)
foreach (var department in departments)
{
department.Dispose();
}
}
}
public class Department : IDisposable
{
//Department makes no sense if it isn't connected to exactly one
//University (composition)
private University uni;
private string name;
//list of Professors can be added to, meaning that one professor could
//be a member of many departments (aggregation)
public IList<Professor> Professors { get; set; }
// internal constructor since a Department makes no sense on its own,
//we should try to limit how it can be created (composition)
internal Department(University uni, string name)
{
this.uni = uni;
this.name = name;
}
public void Dispose()
{
//destroy the department, but let the Professors worry about
//themselves (aggregation)
}
}
public class Professor
{
}
回答by Marc Gravell
Well, in a garbage collected world where all objects are accessed via references, the subtle difference (in ownership) between strict composition and aggregation blurs a little - so in general(when using classes) it boils down to a field for the other object or collection:
好吧,在所有对象都通过引用访问的垃圾收集世界中,严格组合和聚合之间的微妙差异(所有权)有点模糊 - 所以一般来说(当使用类时)它归结为另一个对象的字段或收藏:
class Building {
private readonly List<Room> rooms = new List<Room>();
public IList<Room> Rooms {get {return rooms;}}
public Address Address {get;set;}
//...
}
class Address {
public string Line1 {get;set;}
public string PostCode {get;set;}
//...
}
class Room {
public string Name {get;set;}
public int Capacity {get;set;}
//...
}
If I've missed the point, please clarify...
如果我错过了重点,请澄清...
Things are more involved when you discuss struct
s - but generally when talking about OO concepts, struct
usage is limited to value fields...
当您讨论struct
s时,事情会更复杂- 但通常在谈论 OO 概念时,struct
用法仅限于值字段......