前言:
這是一篇關于Java中關聯,組合和聚合的翻譯,原文地址在這里。我會一段一段地給出原文,然后在下面給出翻譯,翻譯這篇文章,旨在學習交流。翻譯中出現的圖片和代碼,都直接從原文引用,版權歸原文作者所有。
我個人覺得,原文中關于組合(Composition )的例子:圖書館和書,舉得并不準確,這應該是一個聚合的例子。但其對關聯、組合、聚合含意的描述也有些獨到的見解。兼聽則明,偏信則暗,盡信書不如無書,其中正誤還需讀者明查。
原文:
Association
Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.
譯文:
關聯
關聯是兩個獨立的類之間,通過它們的對象建立的關系。關聯可以是一對一,一對多,多對一,多對多。
在面向對象編程中,一個對象使用其他對象提供的方法和服務與它通信。組合和聚合是兩種形式的關聯。
// Java program to illustrate the
// concept of Association
import java.io.*;
// class bank
class Bank
{
private String name;
// bank name
Bank(String name)
{
this.name = name;
}
public String getBankName()
{
return this.name;
}
}
// employee class
class Employee
{
private String name;
// employee name
Employee(String name)
{
this.name = name;
}
public String getEmployeeName()
{
return this.name;
}
}
// Association between both the
// classes in main method
class Association
{
public static void main (String[] args)
{
Bank bank = new Bank("Axis");
Employee emp = new Employee("Neha");
System.out.println(emp.getEmployeeName() +
" is employee of " + bank.getBankName());
}
}
原文:
Output:
譯文:
輸出:
Neha is employee of Axis
原文:
In above example two separate classes Bank and Employee are associated through their Objects. Bank can have many employees, So it is a one-to-many relationship.
譯文:
在上面的例子中,兩個獨立的類Bank
和 Employee
通過它們的對象關聯。銀行有很多顧員,因此是一個一對多的關系。
原文:
Aggregation
It is a special form of Association where:
- It represents Has-A relationship.
- It is a unidirectional association i.e. a one way relationship. For example, department can have students but vice versa is not possible and thus unidirectional in nature.
- In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity
譯文:
聚合
它是一個特殊的關聯:
- 它代表
‘has-a’(有一個)
關系。 - 它是一個單向的關聯,例如:一種單方面的關系. 舉個例子: 院系可以有多個學生,但反過來就不可能(譯注:學生不可能有多個學院),這本質上是單向關聯.
- 在聚合中,兩個類的實例可以單獨存活,這意味著,結束一個實例不會影響其他實例.
// Java program to illustrate
//the concept of Aggregation.
import java.io.*;
import java.util.*;
// student class
class Student
{
String name;
int id ;
String dept;
Student(String name, int id, String dept)
{
this.name = name;
this.id = id;
this.dept = dept;
}
}
/* Department class contains list of student
Objects. It is associated with student
class through its Object(s). */
class Department
{
String name;
private List<Student> students;
Department(String name, List<Student> students)
{
this.name = name;
this.students = students;
}
public List<Student> getStudents()
{
return students;
}
}
/* Institute class contains list of Department
Objects. It is asoociated with Department
class through its Object(s).*/
class Institute
{
String instituteName;
private List<Department> departments;
Institute(String instituteName, List<Department> departments)
{
this.instituteName = instituteName;
this.departments = departments;
}
// count total students of all departments
// in a given institute
public int getTotalStudentsInInstitute()
{
int noOfStudents = 0;
List<Student> students;
for(Department dept : departments)
{
students = dept.getStudents();
for(Student s : students)
{
noOfStudents++;
}
}
return noOfStudents;
}
}
// main method
class GFG
{
public static void main (String[] args)
{
Student s1 = new Student("Mia", 1, "CSE");
Student s2 = new Student("Priya", 2, "CSE");
Student s3 = new Student("John", 1, "EE");
Student s4 = new Student("Rahul", 2, "EE");
// making a List of
// CSE Students.
List <Student> cse_students = new ArrayList<Student>();
cse_students.add(s1);
cse_students.add(s2);
// making a List of
// EE Students
List <Student> ee_students = new ArrayList<Student>();
ee_students.add(s3);
ee_students.add(s4);
Department CSE = new Department("CSE", cse_students);
Department EE = new Department("EE", ee_students);
List <Department> departments = new ArrayList<Department>();
departments.add(CSE);
departments.add(EE);
// creating an instance of Institute.
Institute institute = new Institute("BITS", departments);
System.out.print("Total students in institute: ");
System.out.print(institute.getTotalStudentsInInstitute());
}
}
原文:
Output:
譯文:
輸出:
Total students in institute: 4
原文:
In this example, there is an Institute which has no. of departments like CSE, EE. Every department has no. of students. So, we make a Institute class which has a reference to Object or no. of Objects (i.e. List of Objects) of the Department class. That means Institute class is associated with Department class through its Object(s). And Department class has also a reference to Object or Objects (i.e. List of Objects) of Student class means it is associated with Student class through its Object(s).
It represents a Has-A relationship.
譯文:
在這個例子中,一個學院有若干院系,像CSE,EE。每一個院系有若干學生。因此,我們創建了Institute
(學院)類,它有指向單個Department
(院系)對象或一系列Department
(院系)對象(例如對象列表)的一個引用。這意味著,Institute
(學院)類通過Department
院系類的對像與院系類關聯。并且院系類還引用Student
(學生類)的一個或多個對象(例如對象列表),意味著它通過 Student
(學生)類的對象與學生類關聯。
原文:
When do we use Aggregation ??
Code reuse is best achieved by aggregation.
譯文:
我們什么時候使用聚合?
代碼復用最好通過聚合實現。
原文:
Composition
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
- It represents part-of relationship.
- In composition, both the entities are dependent on each other.
- When there is a composition between two entities, the composed object cannot exist without the other entity.
原文:
組合
組合是聚合的一個受限制形式,其中的兩個實例之間相互高度依賴。
- 它代表了‘part-of’(一部分)的關系。
- 在組合中,兩個實例之間相互依賴
- 如果兩個實例之間有組合關系,合成對象不能脫離另一個實例存在。
原文:
Lets take example of Library.
譯文:
讓我們用圖書館舉例。
// Java program to illustrate
// the concept of Composition
import java.io.*;
import java.util.*;
// class book
class Book
{
public String title;
public String author;
Book(String title, String author)
{
this.title = title;
this.author = author;
}
}
// Libary class contains
// list of books.
class Library
{
// reference to refer to list of books.
private final List<Book> books;
Library (List<Book> books)
{
this.books = books;
}
public List<Book> getTotalBooksInLibrary(){
return books;
}
}
// main method
class GFG
{
public static void main (String[] args)
{
// Creating the Objects of Book class.
Book b1 = new Book("EffectiveJ Java", "Joshua Bloch");
Book b2 = new Book("Thinking in Java", "Bruce Eckel");
Book b3 = new Book("Java: The Complete Reference", "Herbert Schildt");
// Creating the list which contains the
// no. of books.
List<Book> books = new ArrayList<Book>();
books.add(b1);
books.add(b2);
books.add(b3);
Library library = new Library(books);
List<Book> bks = library.getTotalBooksInLibrary();
for(Book bk : bks){
System.out.println("Title : " + bk.title + " and "
+" Author : " + bk.author);
}
}
}
原文:
Output
譯文:
輸出
Title : EffectiveJ Java and Author : Joshua Bloch
Title : Thinking in Java and Author : Bruce Eckel
Title : Java: The Complete Reference and Author : Herbert Schildt
原文:
In above example a library can have no. of books on same or different subjects. So, If Library gets destroyed then All books within that particular library will be destroyed. i.e. book can not exist without library. That’s why it is composition.
譯文:
在上面的例子中,一個圖書館可以有一系列相同或不同科目的書,如果圖書館被摧毀,那么里面所有的書會被毀掉。也就是,書不能脫離圖書館存在。這也是成為組合的理由。
原文:
Aggregation vs Composition
譯文:
聚合與組合
原文:
- Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human
- Type of Relationship: Aggregation relation is “has-a” and composition is “part-of” relation.
- Type of association: Composition is a strong Association whereas Aggregation is a weak Association.
譯文:
- 依賴性:聚合意味著一個關系:子實例可以獨立于父實例存在。例如,銀行和顧員,刪除銀行,顧員仍然存在。然而組合意味著一個關系:子實例不可以獨立于父實例存在。例如,人和心臟,心臟不能離開人。
- 關系類型:聚合關系是 “has-a” (有一個),組合關系是“part-of”(一部分)。
- 關聯類型:組合是一個強關聯,而聚合是一個弱關聯。
// Java program to illustrate the
// difference between Aggregation
// Composition.
import java.io.*;
// Engine class which will
// be used by car. so 'Car'
// class will have a field
// of Engine type.
class Engine
{
// starting an engine.
public void work()
{
System.out.println("Engine of car has been started ");
}
}
// Engine class
final class Car
{
// For a car to move,
// it need to have a engine.
private final Engine engine; // Composition
//private Engine engine; // Aggregation
Car(Engine engine)
{
this.engine = engine;
}
// car start moving by starting engine
public void move()
{
//if(engine != null)
{
engine.work();
System.out.println("Car is moving ");
}
}
}
class GFG
{
public static void main (String[] args)
{
// making an engine by creating
// an instance of Engine class.
Engine engine = new Engine();
// Making a car with engine.
// so we are passing a engine
// instance as an argument while
// creating instace of Car.
Car car = new Car(engine);
car.move();
}
}
原文:
Output:
譯文:
輸出:
Engine of car has been started
Car is moving
原文:
In case of aggregation, the Car also performs its functions through an Engine. but the Engine is not always an internal part of the Car. An engine can be swapped out or even can be removed from the car. That’ why we make The Engine type field non-final.
譯文:
在聚合的情形中,Car
(汽車) 能通過一個Engine
(引擎)工作。但是引擎通常不是汽車內部的一部分。一個引擎可以被替換甚至從汽車中移除。這是我們把Engine
類的成員變量定義成non-final
的原因。
原文:
This article is contributed by Nitsdheerendra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
譯文:
這篇文章由Nitsdheerendra貢獻,如果你喜歡GeeksforGeeks并且想投稿,你也可以用 contribute.geeksforgeeks.org 寫一篇文章,或者把你的文章發送到contribute@geeksforgeeks.org。期待你的文章出現在GeeksforGeeks 上,并且能幫助其他極客。
原文:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
譯文:
如果你發現任何錯誤,或者想分享更多關于以上話題的信息,請寫評論。