Friday, December 24, 2010

GRASP IN SOFTWARE



What is the GRASP?

The world is dynamic. Its get changed every day or some one can say that it’s changing each and every time. But truth is, world never change but it’s modified by the adorns which some expert scientist adding on it by the out come of those peoples extreme researches. What so ever at the end people can say it’s a beautiful world? The thing we should note is how it becomes such a beautiful?
Answer is , its open and no restriction ,every people can change the world by putting so many things on it and make that so tedious since there is no restriction. But one day time had come to made a decision to restrict the things which is adding to the surface of the hearth and making to add what really necessary. Now its so beauty because it’s more organized and more compressed way every thing has been put so that is illuminating as precious stone on the galaxy.

Like world, Software also the same.

From the day beginning of the computer , so many people started to research on it and made so many remarkable out comes and put those things in to the open world. So the people who want to develop software they tried with all the softy solutions which they picking up in the world and tried to apply to the software what they tried to develop. But at the end they came up a software solution what they needed which is messy, very complex, combined the module together which is not easy to distinguishable in future if it needs. What so ever time came to make the software is very beautiful and elegant one now a days. The thing we should note is, how it becomes such a beautiful?

Answer is, since software industry is very open and no much restriction, the software developer started to pick most of the solution which is pre proven and tried to apply to the software which they tried to developed. By putting all the pattern, so many design principles, the solution which is developed was very messy and its so hard to even maintains. Why because no body knows how to unfix each component and modify since all are tightly fixed. But now time has come to make to develop the software solution which is so beauty and elegance by putting the limited predefine and designed applicable pattern on it rather putting all at to ones making a component and make it dependent and assigning a unique responsibility to each component but still those are virtually connected to each other. Generally speaking that we are Generalizing Responsibility Assignment to the Software as a Patten (GRASP).
The GRASP consists with few design principles as well as design pattern which create the set of complete methodology where software can be engineered by applying this pattern. But have to remember is, this is not completely a design pattern but its correct if any body argue that this is set of tool set which help to design a OOP software application. We good software engineers always are worrying about the designing of the software to make each component independent and oriented for one particular task.ie a responsibility. In a nutshell, the key thing to good architecture in software is assigning responsibilities to the classes correctly. So name chosen to suggest the importance of grasping fundamental principles to successfully design object-oriented software is GRASP deals exclusively with this question.
Contents of GRASP
1) Informational Expert
2) Creator
3) Controller
4) Low Coupling
5) Higher Cohesion
6) Polymorphism
7) Pure Fabrication
8) Indirection
9) Protected Variation

Informational Expert
The informational expert pattern is: The class which has more information to carry out or solve a problem in its domain context. That’s a responsibility which is to be assigned to the object that has more data to full fill the responsibility easily.
Eg: Lets say , I have responsibility like this
I want to display all java projects an engineer worked on more than one year. So how I am going to assign this responsibility to the classes. So I am going to have three classes like 1) Engineer, 2) WorkedOn 3) Project
1) First responsibility is : get the total project an Engineer worked. This is assigned to the Engineer since he has more data about the project he worked.
2) Second responsibility is : Get the period he worked in a project : This is assigned to the class Worked On ,because this class has the information of the time period about an engineer worked on a project.
3) Third responsibility is : Get all the java project an engineer worked. This responsibility is assigned to the Project class where it has the information of the project and its technology.
Like this we can give the responsibility to each class which has more information to fulfill the responsibility.
The below code snippet shows this pattern
package iExpert;

/**
*
* @author sivakumaran
*
*/
public class Engineer {

//Hold the value of total worked project.
private Integer totoalWorkedProject;

/**
* This method get the total project an engineer worked on.
* @return totoalWorkedProject.
*/
public Integer getTotalProject(){

return totoalWorkedProject;
}
}

package iExpert;

/**
*
* @author sivakumaran
*
*/
public class WorkedOn {

// hold the valeu of period.
private Integer period;

/**
* This method get the time period of the the project worked.
*
* @param eId
* set the engineer id
* @param pId
* set the project id
* @return period
*/
public Integer getTimePeriod(final Integer eId, final Integer pId) {

return period;
}
}

package iExpert;

import java.util.List;

/**
*
* @author sivakumaran
*
*/
public class Project {

// hold the list of project
private List project;

public List getProject() {

return project;
}

public void setProject(List project) {
this.project = project;
}

/**
* This method get all the java project of an emplyee
*
* @param eId
* set the employee id
* @param technolgy
* set the technology eg:: java.c#
* @return list of the project
*/
public List getAllJavaProject(final Integer eId, final String technolgy) {

return getProject();
}
}

Creator
In this, the main responsibility is to be assigned is who should create object of other classes. This is measured by few criteria as below. If one of criteria is match, then that particular class is more vital to create required object.
Let’s assume that, a responsibility has been assigned to the Book class to create the Page class.
1. Book contains or compositely aggregates Page
2. Book records Page
3. Book closely uses Page
4. Book has initializing data for Page that will be passed to Page when its created.
Thus Book has more information about page so that Book is expert to create the Page.

Eg :
package creator;

/**
*
* @author sivakumaran
*
*/
public class Page {

//Hold the value of page title value.
private String pageTitle;

//Hold the value of page contents value.
private String pageContents;

/**
* constructor
* @param pageTitle
* @param pageContents
*/
public Page(final String pageTitle, final String pageContents){

this.pageTitle = pageTitle;
this.pageContents = pageContents;
}
}

package creator;

import java.util.List;

/**
*
* @author sivakumaran
*
*/
public class Book {

//Hold the value of the totoal pages in book
private List numberOfPages;

//Hold the value of the page contents
private String pageContents;

//Hold the value of the page title
private String pageTitle;


public void createPage(){

Page pageObj = new Page(pageTitle, pageContents);
numberOfPages.add(pageObj);
}

public Page getPage(){

return numberOfPages.get(0);
}

}

Controller
Basically this pattern is assigned the responsibility to the object to receiving or handling the system event. That’s UI events. A Controller object is a non-user interface object responsible for receiving or handling a system event.
These events are being handled in two ways.
1 Represent the overall system like façade pattern.
2 Represent a use case scenario where system event occurs.(log when DAO object creates).
Basically these controller most probably not doing much work but delegate it others

Eg: ActionServlet of the Struts framework

Low Coupling
Coupling in the software arena is, what is the weight of dependencies of one object to other. So each designer of the software application , design it in a way as make low dependences to other objects. That’s make that low coupling. Why we need to have low coupling is, its maintainability. That’s we can modify the code later on with low effect of other classes.
So our problem is in GRASP is how to assign the responsibility that may remain low or can complete it without remain. So design module to keep lower dependency to other.

Higher Cohesion
Cohesion is a measure of “relatedness”. High Cohesion says elements are strongly related to one another. Low Cohesion says elements are not strongly related to one another.
Designs with low cohesion are difficult to maintain and reuse. One of the fundamental goals of an effective design is to achieve high cohesion with low coupling. So this is in GRASP context is Assign a responsibility so that cohesion remains high.

Polymorphism
Polymorphism can be talked about in numerous ways. One is - using polymorphism to extend your applications and make them more pluggable. That is the point of the GRASP Pattern and usually the point of all books on object-oriented programming.
Polymorphism in this context entails creating polymorphic methods in base classes / interfaces that are overridden / implemented in concrete or derived classes. In brief - How to handle alternative behavior based upon type.

E.g. Who is responsible for authorizing different types of payments in sale order Domain. So each payment will authorized it self.

package Polimorphism;

/**
*
* @author sivakumaran
*
*/
public class Payment {

/**
* Authorising the payment
*/
public void authorize(){

}
}


class CashPayment extends Payment {

@Override
public void authorize() {
// TODO Auto-generated method stub
super.authorize();
}
}

class CreditPayment extends Payment {

@Override
public void authorize() {
// TODO Auto-generated method stub
super.authorize();
}
}


class CheckPayment extends Payment {

@Override
public void authorize() {
// TODO Auto-generated method stub
super.authorize();
}
}



Pure Fabrication
Sometimes, during design, responsibilities need to be assigned that are not naturally attributable to any of the conceptual classes. Create an artificial class that does not represent anything in the problem domain. There are situations in which assigning responsibilities only to domain classes lead to poor cohesion or coupling or low reuse potential. So assign a highly cohesive set of responsibilities to an artificial class - a fabrication.
What we expect from this is , A class should be designed with high potential of reuse - responsibilities are small and cohesive. This will create a function-centric object. Part of the high-level object-oriented service layer in architecture.
Eg : Most of the GoF patterns involve fabricating new classes like observer,adapter, abstract factory, etc.These are only for process centric and responsible for its task .eg: factory is used for create objects.

Indirection
This pattern basically involve to make object decouple.So in GRASP how this is happen is , assign responsibility to an intermediate object to mediate between other components so that they are not directly coupled.
Eg: All proxy classes.

Protected Variation
This pattern has the responsibility to answer the question of how to minimize the impact of change due to variations or instability of certain objects, subsystems and systems?.
Identify points of predicted variation or instability and assign responsibilities to create a stable interface (or protection mechanism) around them and applying data encapsulation, interfaces, polymorphism and indirection design patterns.
Service Lookup is an example of protected variations because clients are protected from changes in the location of services using the lookup service. Also externalizing properties in a property file.