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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 105: Line 105:
     public void setQuantity(int q) {
     public void setQuantity(int q) {
         this.quantity = q;
         this.quantity = q;
    }
  }
</pre>
<br>
<pre>
  public class ShoppingCart {
    private List orders = new ArrayList();
 
    public void addOrder(Order o) {
        this.orders.add(o);
    }
    public List getOrders() {
        return this.orders;
     }
     }
   }
   }
</pre>
</pre>

Revision as of 02:04, 30 November 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.

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.

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

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.

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.

3. The Shopping Cart total cannot exceed 2000$.

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;
     }
  }