Association, Composition and Aggregation in Java
每次在画UML的时候,总是会把类的关系搞混,特别是在处理组合和聚合的时候,傻傻分不清楚,然后看到了一篇文章,来翻译一下,不过我想,Java编程思想讲的比较好,记得经常温习温习。
原文地址:Association, Composition and Aggregation in Java
Association(关联)
关联指的是两个分开的类通过对象或实例建立的关系。关联可以是一对一,一对多,多对一,还有多对多,就好像数据库中的关联表,外键一样,我觉得可以结合数据库进行理解。
在面向对象的编程当中,一个对象通过调用另一个对象的方法和服务来进行交流合作。下面有一张图形象的表现出关联,组合,聚合的关系。
图片来源于网络geeksforgeeks
很明显关联比较宽泛,组合是聚合的一个子集。组合-》聚合-》关联。
我们先看个例子:
// 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());
}
}
运行结果:
Neha is employee of Axis
在上面的例子中,银行和雇员是两个不同的类,通过对象将它们关联起来,就比如银行里有某某哪个雇员,一个银行可以有很多雇员,相当于是一对多的关系,而这个关系是我们手动关联起来的。
Aggregation(聚合)
聚合是关联的一种特殊形式:主要体现在一下三个方面
- 聚合代表了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());
}
}
输出结果:
Total students in institute: 4
上面这个例子中,一个学院会有很多专业或者部门,每个专业又包含很多学生。因此,学院类中会持有一个专业或部门列表引用,然后专业类中会持有学生列表引用,也就是说,学院类会通过持有专业类的对象与之关联,专业与学生的关系也是如此。都体现了has-a的关系。
使用场景
代码重用时,可以考虑使用聚合。
Composition(组合)
组合是一种聚合的限制形式,其中两个实体高度相互依赖。也就是说,两个类高度耦合。有以下特征:
- 代表这一种part-of的关系,就是一个类是另一个类的一部分
- 在组合的关系中,实例是彼此依赖的。
- 当两个实例是组合关系,它们就是高度耦合的,一个对象必须依赖另一个对象的存在而存在。
听上去有点不好理解,我们再来看以下代码:
// 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);
}
}
}
运行结果:
Title : EffectiveJ Java and Author : Joshua Bloch
Title : Thinking in Java and Author : Bruce Eckel
Title : Java: The Complete Reference and Author : Herbert Schildt
上述例子中,图书管中会有很多相同或不同主题的图书,但是如果图书馆被销毁后,对应里面的图书也被销毁了,图书不能独立于图书馆而存在,图书馆没了,图书也没有了,这就是组合关系。
Aggregation vs Composition(聚合vs组合)
聚合和组合的比较:
- 依赖:聚合代表被关联的一方可以独立于关联一方而存在的关系,比如说,银行和雇员,将银行对象删除后,雇员对象仍然存在。然后,组合代表着被关联的一方不能独立于一方而存在的关系,比如,人没有心脏就无法生存,心脏也是相对人而言的,没有人,也就没有心脏这么一说。
- 关系类型:聚合是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();
}
}
在聚合关系的代码实现中,汽车通过引擎对象来实现部分功能,但是,引擎不是总存在汽车的内部,引擎可以换,并且可以从汽车中卸下来,这也是为什么聚合关系中,引擎对象是非final的。