Information Expert GRASP Pattern

Information expert (also expert or the expert principle) is a principle used to determine where to delegate responsibilities such as methods, computed fields, and so on.

Problem

What is a general principle of assigning responsibilities to objects?

Solution

Assign responsibility to the information expert - the class that has the information necessary to fulfill the responsibility.

Example

Let's consider Point of Sale (POS) application: a brief overview of the POS application.
  • Application for a shop, restaurant, etc. that registers sales.
  • Each sale is of one or more items of one or more product types and happens at a certain date.
  • A product has a specification including a description, unitary price, an identifier.
  • The application also registers payments (say, in cash) associated with sales.
  • Payment is for a certain amount, equal to or greater than the total of the sale. Problem statement: Who's responsible for knowing the grand total of a Sale?
In the POS application, some class needs to know the grand total of a sale.
Start assigning responsibilities by clearly stating the responsibility. By Information Expert, we should look for that class of objects that has the information needed to determine the total. It is necessary to know about all the SalesLineltem instances of a sale and the sum of their subtotals.
A Sale instance contains these; therefore, by the guideline of Information Expert, Sale is a suitable class of object for this responsibility; it is an information expert for the work.
Let's write a sample method in Sale domain model class.
public class Sale {
    //...
   public double getTotal(){
       double total = 0;
       for (SalesLineItem s : salesLineItem) {
           ProductSpecification prodspec
           = s.getProductSpecification();
           total += s.getQuantity()*prodspec.getPrice();
       }
       return total;
    }
}
This is above code is enough? It is not enough right because here we need getSubTotal from salesLineItem. Let's create the method in SaleLineItem domain model class.
public class SalesLineItem {
     //...
     public double getSubTotal(){
         return this.getQuantity()
         productSpecification.getPrice();
  }
 }
Observe ProductSpecification domain model provides getPrice method gives a price. Fulfill responsibility may require information spread across different classes, each expert on its own data.

Benefits

  • Information encapsulation is maintained since objects use their own information to fulfill tasks. This usually supports low coupling, which leads to more robust and maintainable systems. (Low Coupling is also a GRASP pattern that is discussed in the following section).
  • Behavior is distributed across the classes that have the required information, thus encouraging more cohesive "lightweight" class definitions that are easier to understand and maintain. High cohesion is usually supported (another pattern discussed later).

Comments

  1. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website designing Pakistan

    ReplyDelete

Post a Comment