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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 19: Line 19:
===GRASP===
===GRASP===


[http://davidhayden.com/blog/dave/archive/2005/03/27/895.aspx GRASP] stands for General Responsibility Assignment Software Pattern Describe fundamental principles of object design and responsibility and is expressed in patterns. Information Expert in one of the five GRASP Patterns, viz. Creator,  
[http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29 GRASP] stands for General Responsibility Assignment Software Pattern Describe fundamental principles of object design and responsibility and is expressed in patterns. Information Expert in one of the five GRASP Patterns, viz. Creator,  
Information Expert, Low Coupling, Controller and High Cohesion.
Information Expert, Low Coupling, Controller and High Cohesion. All patterns are solutions to some common problems faced during software development.


===Responsibility Driven Design (RDD)===
===Responsibility Driven Design (RDD)===


*A way of thinking about OOD:
*[http://en.wikipedia.org/wiki/Responsibility-driven_design Responsibility driven design] is the design techniques using in Object Oriented Design. It basically focuses on two aspects of an object
#Responsibilities
#What information does this object share?
#Roles
#What actions is this object responsible for?
#Collaborations
*Some of the Common responsibility categories include
*Common responsibility categories:
#Doing:
#Doing:
##Controlling and coordinating activities in other objects
##Doing something itself:
##Doing something itself:
###Creating and object or doing a calculation
###Creating and object or doing a calculation
##Initiating action in other objects
##Initiating action in other objects
##Controlling and coordinating activities in other objects
#Knowing:
#Knowing:
##Knowing about private data
##Knowing about private data
##Knowing about things it can derive or calculate
##Knowing about related objects
##Knowing about related objects
##Knowing about things it can derive or calculate
##Bigger responsibilities may take several classes
##Bigger responsibilities may take several classes
*Guideline:
*Guideline:
Line 184: Line 183:
# http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html
# http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/InformationExpert.html
# http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/InformationExpert.html
# http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29
# http://en.wikipedia.org/wiki/Responsibility-driven_design

Revision as of 00:22, 12 December 2010

Information Expert Pattern

Introduction

There is always a problem of assuming which object should be given a responsibility and this is where information expert pattern comes into play. we assign responsibilities to classes during the design phase and information expert helps us decide which responsibility should be given to a class.

Information Expert is defined as : Assign a responsibility to the information expert; the class that has the information necessary to fulfill the responsibility.

we need to analyze how much effect Information expert causes to cohesion and coupling before deciding the pattern for the class.

Pattern

A pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice

In a nutshell, the principle of Information Expert is to assign responsibilities and to look at a given responsibility, determine the information needed to fulfill it, and then determine where that information is stored.Then, Information Expert will lead to placing the responsibility on the class with the most information required to fulfill it.

GRASP

GRASP stands for General Responsibility Assignment Software Pattern Describe fundamental principles of object design and responsibility and is expressed in patterns. Information Expert in one of the five GRASP Patterns, viz. Creator, Information Expert, Low Coupling, Controller and High Cohesion. All patterns are solutions to some common problems faced during software development.

Responsibility Driven Design (RDD)

  1. What information does this object share?
  2. What actions is this object responsible for?
  • Some of the Common responsibility categories include
  1. Doing:
    1. Controlling and coordinating activities in other objects
    2. Doing something itself:
      1. Creating and object or doing a calculation
    3. Initiating action in other objects
  2. Knowing:
    1. Knowing about private data
    2. Knowing about things it can derive or calculate
    3. Knowing about related objects
    4. Bigger responsibilities may take several classes
  • Guideline:
  1. Domain model helps with “knowing”
  2. Interaction diagrams help with “doing”

Example

Monopoly

Let us consider Monopoly game for example. There are 6 classes among which the responsibilities have been shared. Information Expert Pattern is used here to decide on the individual responsibility of each class as shown in the diagram.



Monopoly class requests service from the Dice object and Board objects as they carry out their domain specific functions. This clear separation of responsibility results in low coupling and also ensures encapsulation and data security. Similarly, Property class takes the responsibility of calculating the mortgage amount and does property specific functions because it has the information required. The prototype for each class is as below.


   import java.util.ArrayList;
   public class Board 
   {
     ArrayList<Square> squares = new ArrayList<Square>();
     //....
     public Square getSquareInfo(int position)
     {
       //.....
       return null;
     } 
   }


   public class Dice 
   {
     private int dice1;
     private int dice2;
 
     public int getFaceValue()
     {
       dice1 = (int)(1+(Math.random()*6));
       dice2 = (int)(1+(Math.random()*6));
       return dice1+dice2;
     }
   }


   public class Monopoly 
   {
     Square current,previous;
     int facevalue;
     int rollDice()
     {
       facevalue = (new Dice()).getFaceValue();
       return facevalue;
     }
     public Square movePlayer(int facevalue,Square prev)
     {
       // move player to appropriate square
       return null;
     }
     //other methods	
     public void Play(Player p1,Player p2)
     {
       //....
     }
     public static void main(String args[])
     {
       Player p1 = new Player();
       Player p2 = new Player();
       Monopoly monopoly = new Monopoly();
       monopoly.Play(p1, p2);
     }
    }


    import java.util.ArrayList;
    public class Player 
    {
       ArrayList<Property> propertyOwned = new ArrayList<Property>();
       ArrayList<Property> propertyMortgage = new ArrayList<Property>();
       void buyProperty()
       {
         //...
       }
       void MortgageProperty()
       {
          //...
       }	
     }


   public class Property 
   {
     Square InSquare;
     String Name;
     int Price;
     int PriceOfHotel;
     int PriceOfHouse;
     int MortgageValue;
      //....
     int MvalueOfHouse;
     int MvalueOfHotel;
     int HousesBuilt;
     int HotelsBuilt;
     Player Owner;
     int getPrice()
     {
       return Price;
     }
     String getName()
     {
       return Name;
     }
     public int calculateMontgageValue()
     {
       return	MortgageValue + (HousesBuilt*PriceOfHouse)+(HotelsBuilt*PriceOfHotel);
     }
   }


   public class Square 
   {
     Property property;
     int position;	
   }


Advantages & Disadvantages

Advantages

  • 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.
  • Behavior is distributed across the classes that have the required information, encouraging more cohesive “lightweight” class definitions that are easier to understand and maintain.
  • High cohesion is usually supported.


Disadvantages

The disadvantages of using the Information Expert pattern are :

  • Sometimes information expert solution leads to coupling & cohesion problems
  • Persistence of objects – object is expert on what needs to be saved
    • Would have to add persistence knowledge – violate coupling & cohesion principles
    • Would be violation of layering principles

References

  1. http://davidhayden.com/blog/dave/archive/2005/03/27/895.aspx
  2. http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29
  3. http://www.augustana.ab.ca/~mohrj/courses/2003.fall/csc220/lecture_notes/responsibilities.html
  4. http://web.cs.wpi.edu/~gpollice/cs4233-a05/CourseNotes/maps/class4/InformationExpert.html
  5. http://en.wikipedia.org/wiki/GRASP_%28object-oriented_design%29
  6. http://en.wikipedia.org/wiki/Responsibility-driven_design