Introduction
The relationship between objects defines how these objects will interact or collaborate to perform an operation in an application. In any application, objects of user interface classes interact with the business layer objects in order to perform an operation. And in the background, the business layer objects may interact with objects of some repository which in turn talks to some data source object or service object.
Case Study
Before we go ahead, let's consider a Customer Order Management application that manages the orders placed by its customers for some products. Each order can have one or more products. The first is to sort out the independent classes from the given description. The following classes can be derived by using single responsibility principle.
- Customer: represents a customer who can place an order for any product
- Address: represents the address of a customer or the shipping address of an order
- Order: represents an order placed by a customer
- OrderItem: represents a product for which the order was placed by the customer
- Product: represents the product itself
- CustomerRepository: this class has the responsibility to serialize (save) and deserialize (retrieve) the customer to and from the data source
- OrderRepository: this class has the responsibility to serialize (save) and deserialize (retrieve) the order to and from the data source
- ProductRepository: this class has the responsibility to serialize (save) and deserialize (retrieve) the product to and from the data source
Types of Object Relationships
The basic types of relationships in object-oriented programming are.
- Collaboration
- Aggregation
- Inheritance
Let's explore them in more detail.
Collaboration
The collaboration relationship is sometimes referred as "uses a" relationship. Two objects are said to be collaborating when one object makes use of another unrelated object to complete an operation.
In our application, in order to save and retrieve the customer details, the CustomerRepository uses a Customer object to serialize (save) and deserialize (retrieve) the data to and from the data source. Similarly, other repository classes like ProductRepository and OrderRepository uses Product and Order objects respectively and therefore are said to be collaborating to perform an operation.
public class Customer
{
public Customer() : this(0)
{ }
public Customer(int customerId)
{
this.CustomerId = customerId;
AddressList = new List<Address>();
}
public int CustomerId { get; private set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public List<Address> AddressList { get; set; }
public bool Validate()
{
bool isValid = true;
if (string.IsNullOrWhiteSpace(FullName) ||
string.IsNullOrWhiteSpace(EmailAddress))
isValid = false;
return isValid;
}
}
public class CustomerRepository
{
// Retrieves a single customer
public Customer Retrieve(int customerId)
{
// Create an instance of the Customer Class
Customer customer = new Customer(customerId);
// Code that retrieves the defined Customer
// Populate the customer object and return
return customer;
}
// Retrieves all customers
public IEnumerable<Customer> Retrieve()
{
// Code that retrieves all customers
return new List<Customer>();
}
// Saves the current customer
public bool Save(Customer customer)
{
// Code that saves the defined customer
return true;
}
}


Dharmraj ThakurPosted Jul 25, 2017, 12:49 AM
Very nice @Gaurav Gahlot