类定义中的聚合和组合表示? [英] Aggregation and Composition representation in class definition?

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

问题描述

聚合是整体/部分关系.如果整体不再存在,但部分仍然存在

Aggregation is a whole/part relationship. If whole does not exist no longer, but part will still exist

但在组合中如果整体不再存在,但部分将不再存在

But in composition If whole does not exist no longer, but part will no longer exist

例如,一所大学拥有多个系(例如化学),每个系都有多个教授.如果大学关闭,院系将不复存在,但这些院系的教授将继续存在.因此,大学可以被视为系的组合,而系则是教授的集合.

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.

我的问题是,我们将如何在 Java 中实际定义大学、系和教授的类定义,这些定义也描述了上述聚合和组合行为?

My question here how we will actually define the class definition of University,Department and Professors in java which also depicts above aggregation and composition behavior?

推荐答案

组合和聚合的概念在 UML 中用带菱形的箭头显示,但是当这些概念用编程语言实现时,可能会从一种编程语言变为另一个.

The concepts of composition and aggregation are displayed with arrows with diamonds in UML, but when those concepts are implemented in a programming language, may change from one programing language to another.

一般来说,在 Java、C# 或 Delphi 等编程语言中,这两种情况都由对象引用表示.每个对象都有一个生命周期"(创建"、做事"、销毁").

Generally speaking, in programming languages, like Java, C# or Delp both cases are represented by object references. Each object has a "life cycle" ("created", "do stuff", "destroyed").

package Universities;

class ProfessorClass {
  string FirstName;
  string LastName;

  // constructor
  public ProfessorClass(string AFirstName, string ALastName) {
    FirstName = AFirstName;
    LastName = ALastName;
  } 
} // class ProfessorClass

class DepartmentClass {
  string Name;
  string Description;

  // constructor
  public DepartmentClass(string AName, string ADescription) {
    Name = AName;
    Description = ADescription;
  } 
} // class DepartmentClass

class UniversityClass {
  // here doesn't look different:

  // aggregation
  ProfessorClass[] Professors;
  // composition
  DepartmentsClass[] Departments;

  // constructor
  public UniversityClass() {
    // "UniversityClass" is in charge of creating parts

    // here doesn't look different:

    DepartmentsClass = new DepartmentsClass[]();

    ProfessorClass = new ProfessorClass[]();
  } 

  public addDepartment(string AName, string ADescription)
  {
  // composition, whole class is in charge of adding parts:
    DepartmentClass Dept = new DepartmentClass(AName, ADescription);
    DepartmentsClass.add(Dept);
  }

  public deleteDepartment(string AName)
  {
  // composition, whole class is in charge of deleting parts:
    DepartmentsClass.delete(AName);
  }

  public addProfessor(ProfessorClass AProfessor)
  {
    // aggregation, whole class only reference other objects,
    // but, may look like they where parts
    ProfessorClass.add(AProfessor);
  }

  public static void main(String[] args) {
    UniversityClass MyUniversity = new UniversityClass();

    // composition, internal, maintained by main class
    MyUniversity.addDepartment("History", "Very Boring");
    MyUniversity.addDepartment("Music", "Only if you like it");
    MyUniversity.addDepartment("Astronomy", "night living people");

    // aggregation, external, referenced by main class,
    // maintained independently
    ProfessorClass Professor1 = new ProfessorClass("Jane", "Doe");
    ProfessorClass Professor2 = new ProfessorClass("Mike", "Smith");
    ProfessorClass Professor3 = new ProfessorClass("Louise", "Kent");

    MyUniversity.addProfessor(Professor1);
    MyUniversity.addProfessor(Professor2);
    MyUniversity.addProfessor(Professor3);  
  } // static void main(...)

} // class UniversityClass

在组合中,由其他对象(部分")组合而成的整体"对象负责创建、使用 &破坏它的部分.整体"对象负责其每个部分"的生命周期".

In composition, "whole" objects that are composed by other objects ("parts"), are in charge of creating, using & destroying its parts. The "whole" object its responsible for the "life cycle" of each of its "parts".

在聚合中,引用其他对象(聚合")的整体"对象,可能看起来类似于部分"(组合),通常不是由主对象创建或销毁,只是分配或取消分配.主对象不直接控制其他对象,只是添加或删除引用.

In aggregation, "whole" objects that reference other objects ("aggregation"), that may look similar to "parts" (composition), usually, are not created or destroyed by the main object, just assigned or deassigned. The main object doesn't control directly the other objects, just add or remove references.

有时您可能会选择其他开发人员已经完成的代码,并且可能会看到两种想法、混淆,并且无法区分使用的概念.

There are some times you may pick code alredy done from other developers, and may see both ideas, mixup, and cannot difference which concept is been used.

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

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