Consultation (object-oriented programming)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

Consultation in object-oriented programming occurs when an object's method implementation consists of a message send of the same message to another constituent object.


class CustomerList
  List<Customer> customers = new ArrayList<Customer>();
  public void add(Customer customer)
    customers.add(customer); // this is a consultation

In this example, the add method of CustomerList consults the List instance to implement the semantics of adding a value to the list. Consultation can be very useful if extra conditions or side-effects have to occur on the method invocations. For instance in this example, the add method can be used to check if the customer is not yet in the list, and to check for non-null customer object.


Consultation is often incorrectly referred to as delegation. The main differences with delegation are that consultation is explicit in the code not a language mechanism as such, and that consultation does not preserve late binding of self whereas delegation does.