CSC/ECE 517 Fall 2010/ch7 7b vk: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 196: Line 196:
By refactoring using Information Expert Pattern, we'll enhance the encapsulation and hence the design will be more robust, as we can place constraints pertaining to any of these responsibilities very easily in their respective '''Information Expert''' classes.
By refactoring using Information Expert Pattern, we'll enhance the encapsulation and hence the design will be more robust, as we can place constraints pertaining to any of these responsibilities very easily in their respective '''Information Expert''' classes.


The above example captures the essence of Information Expert Pattern. We should observe that though, we assigned the responsibility to the Information Expert, which has the required information to fulfill it, the task is generally accomplished by collaborating with several other classes.
The above example captures the essence of Information Expert Pattern. We should observe that though, we assigned the responsibility to the Information Expert, which has the required information to fulfill it, the task is often accomplished by collaborating with several other classes. This implies there are many 'partial' information experts collaborating in fulfilling the assigned responsibility.


In our example, since the class '''ShoppingCart''' has the list the orders, Expert Pattern suggests us to assign the responsibility of calculating the total to it. But, this responsibility is fulfilled by passing a message to each of the Order objects to calculate the subTotal
In our example, since the class '''ShoppingCart''' has the list of orders, Expert Pattern suggests us to assign the responsibility of calculating the total to it. But, this task needs collaboration of three classes as illustrated by the following figure.


[[Image:Wiki7_2.png|thumb|center|720x280px|alt=Shopping Cart Example|Shopping Cart Example]]
[[Image:Wiki7_2.png|thumb|center|720x280px|alt=Shopping Cart Example|Shopping Cart Example]]

Revision as of 20:40, 1 December 2010

Information Expert Pattern is one of the GRASP patterns which states the following:

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

Object Oriented Design

Object oriented design is a process of building a system of interacting objects to solve a given software problem. It can be otherwise defined as, identifying requirements and creating a domain model, then adding methods to the software classes, and defining the messaging between the objects to fulfill the requirements. But, deciding what methods belong where, and how the objects should interact, is critically important in the designing process and is something non-trivial.

Design Patterns

Experienced Object-oriented developers build up a repertoire of both general principles and idiomatic solutions that guide them during Object oriented design phase of the software. The principles and idioms, if codified in a structured format describing the problem and solution, and given a name, may be called as Design patterns.

The benefits of design patterns include [1] :

  • Code is more accessible and understandable.
  • Use of common vocabulary to help collaboration among developers.
  • Helps people understand the system more quickly.
  • Easier to modify system.

GRASP

General Responsibility Assignment Software Patterns (or Principles), popularly known as GRASP, are a set of guidelines for assigning responsibility to software classes and objects in object oriented design. Information Expert Pattern is one of the basic five GRASP Patterns, Information Expert, Creator, High Cohesion, Low Coupling and Controller, which address very basic, common questions and fundamental design issues.

Our focus in this article will be Information Expert Pattern. Information Expert Pattern is also known as Expert Pattern.

Responsibilities and Methods

The OMG in its UML standard specification defines a responsibility as "a contract or obligation of a classifier". These responsibilities are of two types.

  • doing
  • knowing

Doing responsibilities of an object include:

  • doing something itself, such as creating an object or doing a calculation
  • initiating action in other objects
  • controlling and coordinating activities in other objects

Knowing responsibilities of an object include:

  • knowing about private encapsulated data
  • knowing about related objects
  • knowing about things it can derive or calculate

The challenge here is to translate these responsibilities into methods and assigning them to an appropriate class such that the design has low coupling and high cohesion. Information Expert Pattern helps the designers in this aspect.

Information Expert Pattern

Information Expert pattern can be simply defined as follows:

Pattern Name : Information Expert Pattern

Problem : What is the basic principle by which we can assign responsibilities to objects?

Solution : Assign a responsibility to the class which has the information needed to fulfill it.

The Shopping Cart Example

Let us consider a simple Shopping Cart example, to demonstrate what exactly Information Expert Pattern guides us.

Shopping Cart Example
Shopping Cart Example

A Shopping Cart can hold one or more Orders, and an Order refers just to a single product. The simple business rules regarding the entities, Shopping Cart, Order and Product are as follows:

1. The Shopping Cart total is sum of all Order subTotals.

2. Order subTotal is calculated by multiplying the price of the Product with the ordered quantity.

Now, given the simple business rules, lets design the classes.

   public Class Product{

     private String productName;

     private double price;

     public double getPrice(){
        return price;
     }

     public void setPrice(double price){
        this.price = price;
     }

  }


  public Class Order{
  
     private Product product;
 
     private int quantity;

     public product getProduct(){
        return this.product;
     }

     public void setProduct(Product p) {
        this.product = p;
     }
 
     public int getQuantity() {
        return this.quantity;
     }

     public void setQuantity(int q) {
        this.quantity = q;
     }
  }


  public class ShoppingCart {

     private List orders = new ArrayList();
  
     public void addOrder(Order o) {
        this.orders.add(o);
     }
 
     public List getOrders() {
        return this.orders;
     }
  }

As we can see, each of the classes have right properties and dependencies:

1. A Product has an attribute 'price'.

2. An Order is composed of a Product and has a product 'quantity'.

3. The Shopping Cart has one or more orders.

4. The Shopping Cart total is calculated by computing all 'Order's subTotal and adding them up.In our implementation these orders are stored in an ArrayList, which can be traversed and each subTotal for the corresponding Order can be added to compute the final 'total'.

5. Order subTotal is calculated by multiplying the price of each Product with the product quantity.

In the above example, we only have various get and set methods but, we actually did not assign any responsibilities to any of the classes.

Now, we will seek the help of Information Expert Pattern to assign responsibilities to most appropriate classes.

Refactoring using Information Expert Pattern

We have two responsibilities to be assigned, which are:

1. The 'total' for the shopping cart has to be computed : we see that class ShoppingCart has the whole list of orders and hence as per Information Expert Pattern, the information expert is the class ShoppingCart.

2. The 'subtotal' for each order has to be computed: we see that class Order has reference to the product and its quantity and hence according to Information Expert Pattern, the information expert is the class Order.

public class Order {

   private Product product;
   private int quantity;
   
   public void setProductAndQuantity(Product p, int q) {
      this.product = p;
      this.quantity = q;
   }
 
   public double calculateSubTotal() {
      return this.product.getPrice() * this.quantity;
   }
}


public class ShoppingCart {

   private List orders = new ArrayList();
  
   public void addOrder(Order o) {
      this.orders.add(o);
   }
 
   public double calculateTotal() {
      double total = 0;
      Iterator it = this.orders.iterator();
      while (it.hasNext()) {
          Order current = (Order) it.next();
          total += current.calculateSubTotal();
      }   
  
      return total;
   }
}

By refactoring using Information Expert Pattern, we'll enhance the encapsulation and hence the design will be more robust, as we can place constraints pertaining to any of these responsibilities very easily in their respective Information Expert classes.

The above example captures the essence of Information Expert Pattern. We should observe that though, we assigned the responsibility to the Information Expert, which has the required information to fulfill it, the task is often accomplished by collaborating with several other classes. This implies there are many 'partial' information experts collaborating in fulfilling the assigned responsibility.

In our example, since the class ShoppingCart has the list of orders, Expert Pattern suggests us to assign the responsibility of calculating the total to it. But, this task needs collaboration of three classes as illustrated by the following figure.

Shopping Cart Example
Shopping Cart Example