< Previous Next >
OOPS Tutorial
1. Overview
In this article, we will learn the important object-oriented concept Aggregation.
Aggregation is an association represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.
2. Intent/Definition
Aggregation is an association represents a part of a whole relationship where a part can exist without a whole. It has a weaker relationship.
It is a specialized form of Association where all object has their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship.
Let’s take an example of the relationship between Department and Teacher. A Teacher may belong to multiple departments. Hence Teacher is a part of multiple departments. But if we delete a Department, Teacher Object will not destroy.
Key Points
- It represents the Has-A relationship.
- It is a unidirectional association i.e. a one-way relationship. For example, the 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 affect the other entity.
3. Implementation
Let's take an example of Line item and their Products. If line-item HAS-A product, then a line item is a whole and product is a part.
If a line item is deleted, then corresponding product needs not to be deleted.
Step 1: Create a Product class.
class Product {
private int id;
private String name;
private String description;
public Product(int id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Product [id=" + id + ", name=" + name + ", description=" + description + "]";
}
}
Step 2: This is LineItem class, which HAS-A aggregation association with Product class. That means, if you delete LineItem, then associated Product can exist.
class LineItem {
private int id;
private int quantity;
private Product p;
public LineItem(int id, int quantity, Product p) {
super();
this.id = id;
this.quantity = quantity;
this.p = p;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public Product getP() {
return p;
}
public void setP(Product p) {
this.p = p;
}
@Override
public String toString() {
return "LineItem [id=" + id + ", quantity=" + quantity + ", p=" + p + "]";
}
}
Step 3: Let's test an Aggregation.
public class Aggregation {
public static void main(String[] args) {
// Create Products
Product p1 = new Product(1, "Pen", "This is red pen");
Product p2 = new Product(2, "Pencil", "This is pencil");
Product p3 = new Product(3, "ColorBox", "This is color box");
// Create lineItem and add quntity of the products
LineItem item1 = new LineItem(1, 2, p1);
LineItem item2 = new LineItem(1, 2, p2);
LineItem item3 = new LineItem(1, 2, p3);
// Before deleting line item 1
System.out.println(item1.getId());
System.out.println(item1.getQuantity());
System.out.println(item1.getP());
item1 = null;
// Still product exist and not deleted
System.out.println(p1);
}
}
Aggregation vs Composition
Related Oops Posts
Top Core Java Tutorials
- Java Tutorial for Beginners
- 50 Java Keywords
- JDBC 4.2 Tutorial
- All Java/J2EE Tutorial
- Java 8 Tutorial
- Java Collections Tutorial
- Java Exceptions Tutorial
- Java Generics Tutorial
- Java 8 Stream API Tutorial
- Java Wrapper Classes
- Java Arrays Guide
- Java Multithreading Tutorial
- Java Concurrency Tutorial
- Oops Concepts Tutorial
- Java String API Guide
- Java Reflection API Tutorial
- Java I/O Tutorial
- Date and Time API Tutorial
- JUnit 5 Tutorial
- JUnit 4 Tutorial
- Java XML Tutorial
- Google GSON Tutorial
Top Java EE Tutorials
- Spring Security Tutorial
- RabbitMQ Tutorial
- Hibernate ORM 5
- Java Persistence API
- Spring Boot 2 Tutorial
- Spring Core 5 Tutorial
- Spring MVC 5 Tutorial
- Spring Data JPA Tutorial
- Apache HttpClient Tutorial
- Spring Framework 5
- Apache Maven Tutorial
- JAX-RS Tutorial
- Jersey Rest Tutorial
Java Library Tutorials
- Java API Guides
- Java SQL Package Tutorial
- All Java/J2EE Tutorial
- Java Lang Package Tutorial
- Java Util Package Tutorial
- Java Lang Reflect Package Tutorial
- Java Time Package Tutorial
- Java IO Package Tutorial
Top Java Best Practices
- Java Enums and Annotations Best Practices
- Java Generics Best Practices
- JUnit Framework Best Practices
- Java Naming Conventions
- Single Responsibility Principle
- Liskov's Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
- Open Closed Principle
- Oops principles in java
- Restful API Best Practices
- JSP Best Practices
- Guide to JDBC Best Practices
- Collection Best Practices
- String Best Practices in Java
- Exception Handling Best Practices
- Synchronization Best Practices
- Guide to JDBC Best Practices
- Serialization Best Practices
The source code of this post is available on GitHub: Object-Oriented Design Guide
Comments
Post a Comment