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.

Sunday, June 27, 2010

Layerd Architecture of Software



Software architecturing is in a nutshell ,organizing software component which is specific for a one functionality in a system, ie.an architectured system will have multiple component each specific for one kind of functionality. But each component should be very independent from other component and also consistent on its own context.

In term of software development , there are few software architectural methodology available which has been identified by software professionals.So we are in a lucky stage that we can use it without thinking as crazily to identify the proper architecturing framework for our own application.The below appearing some of architecturing methodology which software professional now days using to develop theirs own application.

Most popular software architectural methodologies

  • Client–server model (2-tier, n-tier, peer-to-peer, cloud computing all use this model)
  • Database-centric architecture (broad division can be made for programs which have database at its center and applications which don't have to rely on databases, E.g. desktop application programs, utility programs etc.)
  • Distributed computing
  • Event-driven architecture
  • Front end and back end
  • Implicit invocation
  • Monolithic application
  • Peer-to-peer
  • Pipes and filters
  • Plugin
  • Representational State Transfer
  • Rule evaluation
  • Search-oriented architecture (A pure SOA implements a service for every data access point.)
  • Service-oriented architecture
  • Shared nothing architecture
  • Software componentry
  • Space based architecture
  • Structured (module-based but usually monolithic within modules)
  • Three-tier model/Layerd Architecture (An architecture with Presentation, Business Logic and Database tiers)
in this article I am going to talk about my favorite architecture methodology ie. Three-tier model/Layerd Architecture in details and its pros and cone.

Three-tier model/Layerd Architecture (An architecture with Presentation, Business Logic and Database tiers)

A software layer is a common layer for J2EE application, where each layer is specific for one section of functionality of the system.The layers are organized in the system in such way that providing support and base functionality for other layers.

For example, lets talk about a ferry service system, when a ferry service need to handle the travels , it needs vessel details which is going to be traveled ,passenger or cargo type which going to be load kind of details.These details are basic details which ferry business service needs handle the travel.So those details are given by ferry data access layer.

Layering is not new concept for software industry.For example if you take the network protocols, it has been using this for years.For instant, FTP,Telnet and browse are services which depends on TCP/IP layer of network, so that if you need to modify or technology advancess, you can do it without affecting to the FTP or Tel net configuration.

So in a nutshell we are keep apart the each layer with distinct functionality in a system.The below table outlines the layers which we can identify in a typical layerd architected software application.


Below is layers and its roles

Data Access Object layer - Manage reading ,writing, updating, and delete stored data,Commonly contains JDBC , Hibernate ,IBatis or some other Database connectivity code.

Business logic layer- Mange business processing tool and logic

Value Object Layer - Lightweight structures for related business information These are some times referred to as data transfer object

Deployment layer - Publishes business object capabilities.

Presentation layer - Control display the end user

Architectural component layer - Generic application utilities.Often these objects are good candidates for enterprise side user
The below immage articulate the tipical layerd architechering methodology with component.

Software Layers for J2EE Application
















So lets go the each layer in details.

Data Access Layer.

Data access layer is specific for handling persistent unit of the application.Basically database SRUD operation.It manage access to persistent storage of some type.Simply the persistent storages are relational database,data xml file and data files.
The primary reason to separate the data access from rest of application is . that's easy to switch data sources and share DAOs between application.
A couple of patterns for data objects are most common.The simplest pattern has each persistent object represent as a DAO .The more complex and but more flexible pattern in common use is a factory base pattern.

Value Object Layer

Every applicaion has data items that logically belongs and typically are used togather. Its programmatically canvenient and with enterprise beans performance enhancing to treat this logical group of data items as a seperate object.

Business Logical layer

Object in the business logic layer combine data with business rules, constraints and activities. Business objects should be separated from DAOs, VOs, and deployment layer, such as enterprise beans to maximize the possibility of reuse.Business objects often use and coordinate the activities of multiple data object.

Deployment layer

Object in the deployment layer called deployment wrappers, are the heart of J2EE achitecture.Deployment wrappers publish business object functionality to java classes that could be on separate machine.

Presentation layer

The presentation layer is the section of application responsible for everything end users physically see in the end user interface.This layer can be build using JSP,Servelet,Html,javascrip etc.

Architectural component Layer

Ideally all general utilities and components would be provided JDK.But accordingly your requirement , you will need to use third party libraries and component.All those component are reside in this layer.

Finally this is all about layered architecture.Next blog i will show coding project example for the Layered architecture.